Java 如何读取文本文件的相对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19874066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:58:03  来源:igfitidea点击:

how to read text file relative path

javapathrelative-path

提问by user2945412

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.

我在这里和那里阅读了源代码,但没有使以下代码正常工作。基本上,我希望从文件夹“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();
        }           
    }

采纳答案by willy

This is a valid absolute path (on the systems I'm aware of):

这是一个有效的绝对路径(在我知道的系统上):

    /path/to/directory/../../otherfolder/etc/

So what the other answerwas 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");

回答by Robin Green

This is not correct:

这是不正确的:

new FileReader(getClass().getResourceAsStream ("/DBTextFiles/Administrator.txt"))

You want:

你要:

new InputStreamReader(getClass().getResourceAsStream ("/DBTextFiles/Administrator.txt"))

回答by Kas

In almost all cases you should use the portable forward slash "/"."In every case you should use either the File constructor that accepts a File (parent) & String (file name) or use System.getProperty("file.separator").

在几乎所有情况下,您都应该使用可移植的正斜杠"/"."在每种情况下,您都应该使用接受 File(父)和 String(文件名)的 File 构造函数或使用System.getProperty("file.separator").

回答by user2945412

Now I get it, somewhat answers here & there do help in getting me to the goal. Did a short edit to my code & it worked. Hope it'll also help some poor souls out there.

现在我明白了,这里和那里的一些答案确实有助于让我达到目标。对我的代码进行了简短的编辑并且它起作用了。希望它也能帮助一些可怜的灵魂。

String filePath = new File("").getAbsolutePath();
System.out.println (filePath);

//http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java    
//http://stackoverflow.com/questions/19874066/how-to-read-text-file-relative-path
BufferedReader reader = new BufferedReader(new FileReader(filePath + "/src/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();
}