如何读取文本文件的相对路径 [英] how to read text file relative path
本文介绍了如何读取文本文件的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里阅读了一些资料。那里,但没有得到以下代码工作。基本上,我希望从文件夹'src'中读取一个名为'Administrator'的文本文件。我需要一个相对路径,因为这个项目可能会转移给另一个人。请耐心等待。
I have read sources here & there, but did not get the following code working. Basically, I wish to read a text file, named 'Administrator' from the folder 'src'. I will need a relative path, since this project may be transferred to another person. Please be patient with me.
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();
}
}
推荐答案
这是一个有效的绝对路径(在我所知的系统上):
This is a valid absolute path (on the systems I'm aware of):
/path/to/directory/../../otherfolder/etc/
那么其他答案说,是通过以下方式获取当前目录的路径: / p>
So what the other answer was saying, was to get the path to the current directory with:
String filePath = new File("").getAbsolutePath();
然后将您的相对路径连接到:
Then concatenate your relative path with:
filePath.concat("path to the property file");
这篇关于如何读取文本文件的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文