为什么找不到我的文件? [英] Why can't I find my File?
本文介绍了为什么找不到我的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static void main(String [] args){
String filename =logger.csv;
文件motor_readings = new File(filename);
尝试{
扫描仪inputStream =新的扫描仪(motor_readings);
while(inputStream.hasNext()){
System.out.println(inputStream.next());
}
inputStream.close();
} catch(FileNotFoundException e){
System.out.println(Error:File not Found!);
}
}
然而,这不断给我一个文件不发现错误。
解决方案
如果您现在使用相对路径,文件需要存在于项目根目录中,
考虑这个层次结构:
project /
src / main / java
file.java
logger.csv
新文件(logger.csv)
将无法正常工作。
project /
/ pre>
logger.csv
src / main / java
file.java
新文件(logger.csv)
将现在工作。 (注意,该文件与src目录相邻。)I'm trying to open a CSV file name "logger.csv" which I have saved in the source folder itself.
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!"); } }
However, this keeps on giving me a "File not found" error.
解决方案If you use relative pathing as you are right now - the file needs to exist in the project root, not in the directory of the java file.
Consider this hierarchy:
project/ src/main/java file.java logger.csv
new File("logger.csv")
will not work.project/ logger.csv src/main/java file.java
new File("logger.csv")
will now work. (notice, the file is adjacent to the src directory.)这篇关于为什么找不到我的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文