如何捕获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)热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短