为什么我找不到我的档案?
我正在尝试打开CSV文件名“logger.csv”,我已将其保存在源文件夹中。
public static void main(String[] args) { String filename = "logger.csv"; File motor_readings = new File(filename); try { Scanner inputStream = new Scanner(motor_readings); while (inputStream.hasNext()){ System.out.println(inputStream.next()); } inputStream.close(); } catch (FileNotFoundException e) { System.out.println("Error: File not found!"); } }
但是,这继续给我一个“找不到文件”错误。
如果您现在使用相对路径 – 文件需要存在于项目根目录中, 而不是存在于java文件的目录中。
考虑此层次结构:
project/ src/main/java file.java logger.csv
new File("logger.csv")
将无法正常工作。
project/ logger.csv src/main/java file.java
new File("logger.csv")
现在可以使用了。 (注意,该文件与src目录相邻。)
将文件升级。 在项目的主文件夹中。
要查看文件的预期位置,请将catch子句中的代码更新为:
System.out.println("Error: File not found: " + motor_readings.getAbsolutePath());
把它放在那里并确保在Eclipse中刷新工作区以便可以看到该文件。