java 在文件上使用 BufferedReader 时出现 FileNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10730413/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:17:07  来源:igfitidea点击:

FileNotFoundException when using BufferedReader on a file

javafilebufferedreaderioexceptionfilenotfoundexception

提问by Wuzseen

I'm very confused on getting basic file reading to work with Java. Lots of mixed signals.

我对使用 Java 读取基本文件感到非常困惑。许多混合信号。

I've tried it a couple different ways and I consistently get a not found exception each time. I've checked with a file object set to the current path to print the current directory and I am indeed in the directory the file I'm trying to open is in. The permissions are set so everyone can read. I'm not sure what is going on:

我已经尝试了几种不同的方法,但每次我始终都遇到未找到的异常。我已经检查了一个文件对象设置为当前路径以打印当前目录,我确实在我试图打开的文件所在的目录中。权限设置为每个人都可以阅读。我不确定发生了什么:

BufferedReader infixLines = new BufferedReader ( new FileReader ( "input.infix" ));

This is the line that throws the error, consequently each consecutive line using infixLines also throws an error.

这是引发错误的行,因此使用 infixLines 的每个连续行也会引发错误。

I've tried it using FileInputStream as well and get the same kind of error.

我也尝试过使用 FileInputStream 并得到同样的错误。

That being said simply doing

这就是说简单地做

File file = new File("input.infix");
if ( file.exists() )
    System.out.println( "Exists" );

DOES work.

确实有效。

Very confused.

很困惑。

EDIT: (Stacktrace?)

编辑:(堆栈跟踪?)

ParseInfix.java:13: unreported exception java.io.FileNotFoundException; must be
            BufferedReader infixLines = new BufferedReader(new FileReader (n
                                                           ^
ParseInfix.java:15: unreported exception java.io.IOException; must be caught or
            while ( ( line = infixLines.readLine()) != null )

回答by wattostudios

The exception trace is saying that your code...

异常跟踪是说您的代码...

BufferedReader infixLines = new BufferedReader ( new FileReader ( "input.infix" ));

Could possiblythrow a FileNotFoundExceptionor an IOExceptionif the file doesn't exist, so it wants you to do something to handle this possibility.

如果文件不存在,可能抛出 aFileNotFoundException或 an IOException,因此它希望您做一些事情来处理这种可能性。

The simplest way is to wrap your file-reading code in a try-catchblock like this...

最简单的方法是将您的文件读取代码包装在这样的try-catch块中......

try {
    BufferedReader infixLines = new BufferedReader ( new FileReader ( "input.infix" ));
    // other reading code here
}
catch (FileNotFoundException e){
    System.out.println(e);
}
catch (IOException e){
    System.out.println(e);
}

The exception isn't saying that the file can't be found, it is just saying that ifthe file doesn't exist, what is your code going to do to handle the situation.

例外并不是说找不到文件,只是说如果文件不存在,您的代码将如何处理这种情况。

Ultimately in the catchblock you would want to do something more than just System.out.println(). For example, in a GUI program, you might show a popup message to tell the user that the file doesn't exist.

最终,在catch块中,您将想做的不仅仅是System.out.println(). 例如,在 GUI 程序中,您可能会显示一条弹出消息,告诉用户该文件不存在。

回答by John Woo

this line

这条线

BufferedReader infixLines = new BufferedReader (new FileReader("input.infix"));

searches for File: input.infix, if it not found then it will return FileNotFoundExceptionexception. Make sure that input.infixresides in the directory same with the java file.

搜索 File: input.infix,如果没有找到则返回FileNotFoundException异常。确保input.infix驻留在与java file.

回答by Suraj Chandran

Its a compilation error, not a runtime exception.

它是一个编译错误,而不是运行时异常。

You need to wrap your File related calls within a try-catch block and handle FileNotFounceExceptionand IOException

您需要将与 File 相关的调用包装在 try-catch 块中并处理FileNotFounceExceptionIOException

回答by Flavins

try this

试试这个

String fileName = "input.infix";
File fil = new File(fileName);
String filePath = fil.getAbsolutePath();
BufferedReader infixLines = new BufferedReader(new FileReader(filePath));