所以我试图从我的 Eclipse 项目中的配置文件中读取,但它找不到该文件。这是我的代码。
public void readConfigFile () {
//URL url = Test.class.getClassLoader().getResource("myfile.txt");
//System.out.println(url.getPath());
URL url = getClass().getResource("/config");
System.out.println(url);
try {
BufferedReader read = new BufferedReader(new FileReader(url.toString()));
//read in values for constants from config file
System.out.println(BASE_URL);
BASE_URL = read.readLine().split("\t")[1];
System.out.println(BASE_URL);
read.close();
}
catch (Exception e)
{
System.out.println("Read from config file failed. Terminating program");
System.out.println(e);
System.exit(1);
}
}
当我运行时,它将url
变量打印为:
/Users/myname/Development/workspace/New_API/bin/config
但是在运行 BufferedReader 命令时找不到文件。我得到:
Read from config file failed. Terminating program
java.io.FileNotFoundException:
file:/Users/myname/Development/workspace/New_API/bin/config.txt (No such file or directory)
但是,当我转到代码的 bin 目录时,配置文件就在那里。在根 bin 目录中,遵循 URL 给出的确切路径。是什么搞砸了 Eclipse?