使用FileReader时解决IOException,FileNotFoundException

时间:2013-09-23 20:55:42

标签: java file filereader java-io ioexception

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

输出: -

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对我来说仍有一些唠叨问题:

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

buffread dosent似乎得到了文件名。我收到这个错误:

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

symbol:variable buffread

location: class ParseFileName

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

4 个答案:

答案 0 :(得分:5)

在方法的标题中添加throws FileNotFoundException, IOException。看起来只是抛出IOException就可以解决你的问题,但是将两者结合起来可以让你判断文件存在是否存在问题,或者是否出现其他问题(参见下面的catch语句)。

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

或者,如果您想捕获特定的异常并对其执行某些操作:

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来解决。

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

答案 1 :(得分:1)

您必须在方法throws的签名中添加main语句或在

中包装代码
try {
    ...
} catch (FileNotFoundException e) {
    ...
}

答案 2 :(得分:1)

您的代码可以抛出FileNotFoundExceptionIOException Checked Exception。您需要在try-catch块中包围代码或在main函数中添加throws声明。

答案 3 :(得分:0)

如果无法找到或正确打开文件,BufferReader可能会抛出异常。

此错误消息告诉您需要处理此异常。您可以在try / catch块中包装创建BufferReader的行。这将处理抛出IOException并打印出堆栈跟踪的情况。

public static void main(String[] args) { 
    ParseFileName  superParse = new ParseFileName();  
    BufferedReader buffread;
    try
    {
        buffread= new BufferedReader (new FileReader("file.txt"));
    }
    catch(FileNotFoundException e)
    {
        e.printStackTrace();
    }

    String line;
    while ((line = buffread.readLine())!= null) {
        String fileName = superParse.getName(line);
        System.out.println("\n" + line + "  =>  " + fileName);
    }
    buffread.close();
}

另一个选择是在方法头中添加“throws IOException”。

public static void main(String[] args) throws IOException {
    //...
}

这告诉你的方法的编译器和调用者你选择不处理这个异常,并且有可能抛出它。