使用FileReader时解决IOException,FileNotFoundException [英] Resolving IOException, FileNotFoundException when using FileReader

查看:131
本文介绍了使用FileReader时解决IOException,FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在下面的代码中解决以下异常.我使用BufferedReader的方式有什么问题?我在主要方法中使用了BufferedReader

I've not been able to resolve the following exception in the code below. What is the problem with the way I use BufferedReader? I'm using BufferedReader inside the main method

输出:-

ParseFileName.java:56: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown 

BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));


// ParseFileName  is used to get the file name from a file path 
// For eg: get  - crc.v  from "$ROOT/rtl/..path/crc.v"

import java.util.regex.Pattern;
import java.io.*;

public class ParseFileName { 

  //Split along /'s , and collect the last term. 
  public String getName (String longName) { 
      String splitAt = "/";
      Pattern pattern1 = Pattern.compile(splitAt);
      String[] parts  = pattern1.split(longName);
      System.out.println("\nparts.length =  " + parts.length);

      //Return the last element in the array of strings 
      return parts[parts.length -1]; 
  }

  public static void main(String[] args) { 
    ParseFileName  superParse = new ParseFileName();  
    BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
    String line;
    while ((line = buffread.readLine())!= null) {
        String fileName = superParse.getName(line);
        System.out.println("\n" + line + "  =>  " + fileName);
    }
    buffread.close();

  }

}

更新: 以下作品:

public static void main(String[] args) throws FileNotFoundException, IOException { 

但是尝试.. catch对我来说仍然有些困扰:

However try.. catch still has some nagging issues for me:

try {
BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
    ex.printStackTrace();
} catch (IOException ex2) {
   ex2.printStackTrace();
}

buffread剂量似乎获得了文件名.我收到此错误:

buffread dosent seem to get the file name. I get this error:

javac ParseFileName.java ParseFileName.java:67: cannot resolve symbol

符号:可变buffread

symbol : variable buffread

location: class ParseFileName

while ((line = buffread.readLine())!= null) {

推荐答案

在方法的标题中添加throws FileNotFoundException, IOException.看起来,只需抛出IOException即可解决您的问题,但是将两者结合使用将使您知道文件的存在是否存在问题或是否有其他问题(请参见下面的catch语句).

Add throws FileNotFoundException, IOException in the header of your method. It looks like just throwing the IOException will solve your problem, but incorporating both will allow you to tell if there was a problem with the file's existence or if something else went wrong (see catch statements below).

public static void main(String[] args) throws FileNotFoundException, IOException { 

或者,如果您想捕获特定的异常并对其进行处理:

Alternately, if you'd like to catch a specific exception and do something with it:

try {
    BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
    // Do something with 'ex'
} catch (IOException ex2) {
    // Do something with 'ex2'
}

更新以解决更新的问题:这只是一个简单的范围问题,可以通过在try语句之外声明BufferedReader来解决.

Update to resolve the updated issue: This is just a simple scope problem which can be solved by declaring the BufferedReader outside of the try statement.

BufferedReader buffread = null;
try {
    buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
        ...

这篇关于使用FileReader时解决IOException,FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆