有没有办法可以打印出我们在这种情况下找不到的文件?
try{
in1 = new Scanner(new File(inPath1));
in2 = new Scanner(new File(inPath2));
} catch (FileNotFoundException e){
System.err.println("File not found: " e);
System.exit(0);
}
打印出:
File not found: java.io.FileNotFoundException: test.dat (The system cannot find the file specified)
但我只对文件名感兴趣,而不是整个字符串。
最佳答案
是的,对您的代码进行较小的更改,如下所示:
String path = null; //track file name
try{
in1 = new Scanner(new File(path = inPath1));
in2 = new Scanner(new File(path = inPath2));
} catch (FileNotFoundException e){
System.err.println("File not found: " + path);//get recent file name
System.exit(0);
}
关于java - 确定打开两个文件时找不到哪个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35663607/