Tar*_*que 1 java io intellij-idea
我正在从Eclipse切换到IntelliJ.我在我的项目中包含了一个文件"input.txt",但是当我编写Java代码来访问它时,我得到了一个FileNotFoundException.我究竟做错了什么?
这不是问题,在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/
归档时间: |
|
查看次数: |
2343 次 |
最近记录: |