Java 使用 FileReader 时解决 IOException、FileNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18968709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resolving IOException, FileNotFoundException when using FileReader
提问by Nikhil
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
我无法解决以下代码中的以下异常。我使用 BufferedReader 的方式有什么问题?我在 main 方法中使用 BufferedReader
OUTPUT :-
输出 :-
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();
}
}
UPDATE :The following works:
更新:以下工作:
public static void main(String[] args) throws FileNotFoundException, IOException {
However try.. catch still has some nagging issues for me:
但是 try.. catch 对我来说仍然有一些烦人的问题:
try {
BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex2) {
ex2.printStackTrace();
}
buffread dosent seem to get the file name. I get this error:
buffread dosent 似乎得到了文件名。我收到此错误:
javac ParseFileName.java ParseFileName.java:67: cannot resolve symbol
symbol : variable buffread
符号:可变缓冲区
location: class ParseFileName
while ((line = buffread.readLine())!= null) {
采纳答案by ElliotSchmelliot
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).
添加throws FileNotFoundException, IOException
方法的标题。看起来只是抛出IOException
将解决您的问题,但结合两者将使您能够判断文件的存在是否存在问题,或者是否出现其他问题(请参阅下面的 catch 语句)。
i.e.
IE
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'
}
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
在 try 语句的外部声明来解决。
BufferedReader buffread = null;
try {
buffread = new BufferedReader (new FileReader("file.txt"));
} catch (FileNotFoundException ex) {
...
回答by briarheart
You have to add throws
statement into the signature of method main
or wrap code in
您必须在throws
方法的签名中添加语句main
或将代码包装在
try {
...
} catch (FileNotFoundException e) {
...
}
回答by Hampton Terry
The BufferReader can throw an exception if the file cannot be found or opened correctly.
如果无法正确找到或打开文件,则 BufferReader 可能会引发异常。
This error message is telling you that you need to handle this exception. You can wrap the line where you create the BufferReader in a try/catch block. This will handle the case an IOException is thrown and print out the stack trace.
此错误消息告诉您需要处理此异常。您可以将创建 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();
}
Another option is to add "throws IOException" to your method header.
另一种选择是将“throws IOException”添加到您的方法标头中。
public static void main(String[] args) throws IOException {
//...
}
This tells the compiler and callers of your method that you are choosing to not handle this exception and there is a chance it will be thrown.
这告诉编译器和您的方法的调用者您选择不处理此异常,并且有可能会抛出它。
回答by Rafi Kamal
Your code can throw FileNotFoundException
or IOException
which is Checked Exception. You need to surround your code in a try-catch block or add a throws declaration in your main function.
您的代码可以抛出FileNotFoundException
或IOException
哪个是Checked Exception。您需要将代码括在 try-catch 块中或在主函数中添加 throws 声明。