从IntelliJ中的Java文件中读取

Tar*_*que 1 java io intellij-idea

我正在从Eclipse切换到IntelliJ.我在我的项目中包含了一个文件"input.txt",但是当我编写Java代码来访问它时,我得到了一个FileNotFoundException.我究竟做错了什么?

FileNotFoundException异常

bob*_*bel 5

这不是问题,在IntelliJ中找不到该文件.事实上,你的编译器说,在运行时可能找不到该文件.所以你必须像这样处理FileNotFoundException一个try- catch块:

try {
    BufferedReader reader = new BufferedReader(new FileReader("./input.txt"));
    // read the file
    // ...
} catch (FileNotFoundException e) {
    // do something, when the file could not be found on the system
    System.out.println("The file doesn't exist.");
} catch (IOException e) {
    // do something, when the file is not readable
    System.out.println("The file could not be read.");
} catch (/* other exceptions... */) {
    // do something else on some other exception
    // ...
}
Run Code Online (Sandbox Code Playgroud)

当然,已经知道,将找到该文件,但不是编译器!

基本上阅读有关异常的内容:http://docs.oracle.com/javase/tutorial/essential/exceptions/