【发布时间】:2017-02-10 17:38:07
【问题描述】:
您好,我遇到了一个我找不到解决方案的问题。我要求用户输入文件名,但我得到的输出始终是“无法打开文件”。任何建议将不胜感激。
Scanner reader = new Scanner(System.in);
System.out.println("Enter the name of textfile to be read ( add .txt): ");
String fileName = reader.next();
String line = null;
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
FileNotFoundException 总是被执行,但是为什么?
P.S 如果我将路径更改为特定位置,例如“C:\etc”,它会读取文件。
【问题讨论】:
-
FileNotFoundException 总是被执行,但是为什么? - 因为找不到你提供的路径的文件?
-
如果我被误解了,我会尽快修改我的问题。
标签: java java.util.scanner filenames bufferedreader