【发布时间】:2021-12-21 17:30:59
【问题描述】:
我正在尝试使用 Java 打开一个文件。如果操作不成功,我的程序应该输出The file was not found, please download file with name DateTemp.csv.。这是我的代码:
File myFile = new File("DateTemp.csv"); //File Name
Scanner inputFile = new Scanner(myFile); //scanner to input file name
try {
//Try to display one too many array elements.
myFile = new File("DateTemp.csv");
for (int i = 0; i <= templow.length; i++);
for (int i = 0; i <= temphigh.length; i++);
//System.out.println(temphigh[i]);
//System.out.println(templow[i]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid array index exceeds Array Size."); //catch block to declare the array index was too large
} catch (FileNotFoundException e) {
System.out.println("The File was not Found, please download file with name DateTemp.csv");
System.exit(1);
}
【问题讨论】:
-
你不需要做第二个“myFile = new File...”。
-
您可能会遇到错误,因为您的文件 DateTemp.csv 的路径是错误的。
标签: java filenotfoundexception