如何在 Java 中捕获文件未找到异常?
在使用 FileInputStream、FileOutputStream 和 RandomAccessFile 类时,我们需要将文件的路径传递给它们的构造函数。如果指定路径中不存在文件,则会引发 FileNotFoundException。
示例
public class Sample {
public static void main(String args[]) throws Exception {
File file = new File("myFile");
FileInputStream fis = new FileInputStream(file);
System.out.println("Hello");
}
}输出
Exception in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.<init>(Unknown Source) at a5Exception_Handling.NullPointer.main(NullPointer.java:10)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP