【问题标题】:Resolving IOException, FileNotFoundException when using FileReader使用 FileReader 时解决 IOException、FileNotFoundException
【发布时间】:2013-09-28 21:47:04
【问题描述】:

我无法在下面的代码中解决以下异常。我使用 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 { 

但是 try..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

符号:变量buffread

location: class ParseFileName

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

【问题讨论】:

  • 您是否遇到编译错误或异常?它应该会给你编译错误。
  • 这与BufferedReader 完全无关,与FileReader 也没有太大关系。

标签: java file filereader java-io ioexception


【解决方案1】:

在方法的标题中添加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) {
        ...

【讨论】:

  • 他的代码仍然无法编译,因为line = buffread.readLine() 会抛出IOException
  • 谢谢,我通过使用 public static void main(String[] args) throws FileNotFoundException, IOException { 解决了异常,但我想改用 try .. catch 语法。我会试试的
  • @Nik 没问题。如果这是您要走的路线,请务必将其标记为答案。
  • @ElliotSchmelliot 我仍然看到使用 try..catch 块的(愚蠢的?)错误。你知道那可能是什么吗?
  • @Nik 是的,我明白了。这只是一个简单的范围问题。查看我的更新。
【解决方案2】:

您必须将throws 语句添加到方法main 的签名中或将代码包装在中

try {
    ...
} catch (FileNotFoundException e) {
    ...
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      此错误消息告诉您需要处理此异常。您可以将创建 BufferReader 的行包装在 try/catch 块中。这将处理抛出 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();
      }
      

      另一种选择是将“抛出 IOException”添加到您的方法头。

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

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

      【讨论】:

      • FileReader 抛出异常。 BufferedReader 那时还没有执行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2016-08-18
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 2014-01-28
      相关资源
      最近更新 更多