我在这里和那里都阅读了源代码,但是没有使以下代码正常工作。基本上,我希望从文件夹“ src”中读取一个名为“ Administrator”的文本文件。我将需要一个相对的途径,因为这个项目可能会转移给另一个人。请耐心等待我。
public void staffExists () throws IOException
{
//http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java
BufferedReader reader = new BufferedReader(new FileReader(getClass().getResourceAsStream ("/DBTextFiles/Administrator.txt")));
try
{
String line = null;
while ((line = reader.readLine()) != null)
{
if (!(line.startsWith("*")))
{
System.out.println(line);
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
reader.close();
}
}
这是有效的绝对路径(在我知道的系统上):
/path/to/directory/../../otherfolder/etc/
因此,另一个答案是说,使用以下命令获取当前目录的路径:
String filePath = new File("").getAbsolutePath();
然后将相对路径与:
filePath.concat("path to the property file");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句