简体   繁体   English

用eclipse找不到文件

[英]Can't find files with eclipse

Okay so I scoured this site and a few others looking for the answer to this problem and tried all of the suggestions.好的,所以我搜索了这个网站和其他一些网站,寻找这个问题的答案,并尝试了所有的建议。 Nothing is working and it keeps throwing the File not found exception .没有任何工作,它不断抛出File not found exception

File inFile = new File("PhoneRecords.txt");
String path = inFile.getAbsolutePath();
 try {
    System.setIn(new FileInputStream(path));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
Scanner sc = new Scanner(System.in);

After this it tries to use the Scanner in a method below.在此之后,它尝试以下面的方法使用扫描仪。

public static PhoneCall readNextPhoneCall(Scanner sc){
  return new PhoneCall (sc.nextDouble(), sc.nextInt());
}

The text file is in the same directory that I am working in and I double made sure that I name it correctly in my code.文本文件位于我正在工作的同一目录中,我再次确保在我的代码中正确命名了它。 Please Help.请帮忙。

Creating File with a relative path is always kind of risky.使用相对路径创建File总是有风险的。 Either use an absolute path like /my/absolute/path or use a different method to get your file.要么使用像 /my/absolute/path 这样的绝对路径,要么使用不同的方法来获取您的文件。

Also, why not using this constructor : Scanner(File source)另外,为什么不使用这个构造函数: Scanner(File source)

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File) http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.io.File)

Or better, use this one : Scanner(InputStream source)或者更好,使用这个: Scanner(InputStream source)

And then, use getResourceAsStream to properly retrieve your file然后,使用 getResourceAsStream 正确检索您的文件

InputStream is = getClass().getClassLoader().getResourceAsStream("PhoneRecords.txt");
Scanner sc = new Scanner(is);

Try to put your file here.尝试将您的文件放在这里。

--->bin
--->src
--->PhoneRecords.txt

or specify the location of the file.或指定文件的位置。 For instance,例如,

String path = "C:\\MyFiles\\PhoneRecords.txt";
File inFile = new File(path);

Did you enable the input in the eclipse console?您是否在 eclipse 控制台中启用了输入? Scanner caused me some problems when used from the eclipse console before..之前从 eclipse 控制台使用时,扫描仪给我带来了一些问题..

在此处输入图片说明

Source: https://stackoverflow.com/a/22471175/441907来源: https : //stackoverflow.com/a/22471175/441907

如果文件放在 src 下,请尝试从类路径加载

Scanner scanner=new Scanner(YourClass.class.getResourceAsStream("/package/...pathtofile");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM