1

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

4 回答 4

6

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

IE

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'
}

更新以解决更新的问题:这只是一个简单的范围问题,可以通过声明BufferedReadertry 语句的外部来解决。

BufferedReader buffread = null;
try {
    buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
        ...
于 2013-09-23T20:59:20.723 回答
1

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

try {
    ...
} catch (FileNotFoundException e) {
    ...
}
于 2013-09-23T21:03:26.817 回答
1

您的代码可以抛出FileNotFoundException或者IOExceptionChecked Exception。您需要将代码包含在 try-catch 块中或在主函数中添加 throws 声明。

于 2013-09-23T21:04:35.120 回答
0

如果无法找到或正确打开文件,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 {
    //...
}

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

于 2013-09-23T21:04:12.803 回答