如何读取位于项目文件夹java中的文件

时间:2016-10-22 12:07:09

标签: java io filepath filereader filenotfoundexception

使用eclipse,我的项目文件夹中标题为HackGSU的文件名为bad_words.txt。我想找到文件并读取文件的内容。我看到了这个答案how to read text file relative path,我尝试了这个:

private static String words[][] = new String[3][];
private static int ok = 17 + 2; //Add two for comment & empty line in text file
private static int med = 26 + 2; //Add two for comment & empty line in text file
private static int bad = 430 + 1; //Add one for comment in text file
private static String p = new File("").getAbsolutePath();

public static String[][] openFile() {

    p.concat("/HackGSU/bad_words.txt");

    //Set the a limit for the amount of words in each row
    words[0] = new String[getOk()];
    words[1] = new String[getMed()];
    words[2] = new String[getBad()];

    //Read the text file to add bad words to an array
    try {
        FileReader fr = new FileReader(p);
        //Wrap file
        BufferedReader br = new BufferedReader(fr);

        //Add each line of file into the words array
        for(int i = 0; i < words.length; i++) {             

            for(int j = 0; j < words[i].length; j++) {
                try {
                    words[i][j] = br.readLine();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }

    return words;
}

但是我收到了这个追溯:

java.io.FileNotFoundException: C:\Users\nbrow_000\workspaceProjects\HackGSU (Access is denied)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at gsu.hack.harassment.BadWords.openFile(BadWords.java:28)
at gsu.hack.harassment.HarassFilter.<clinit>(HarassFilter.java:10)
at gsu.hack.harassment.CheckPercentage.main(CheckPercentage.java:20)

Exception in thread "main" java.lang.NullPointerException
at gsu.hack.harassment.HarassFilter.checkHarass(HarassFilter.java:23)
at gsu.hack.harassment.CheckPercentage.main(CheckPercentage.java:20)

我也这样回答How to read a text file directly from Internet using Java?,但我的URL构造函数有问题。

如果我放置本地文件路径(C://.../bad_words.txt)它工作正常,但我怎样才能让程序读取文件,这样如果我打包软件它仍然会找到正确的文件路径。

4 个答案:

答案 0 :(得分:1)

查看这个问题的答案,它可能就是你要找的东西!

How to read file from relative path in Java project? java.io.File cannot find the path specified

答案 1 :(得分:1)

看看这个错误,看起来你并没有得到完整的路径。而不是

p.concat("/HackGSU/bad_words.txt");

尝试

p = p.concat("/HackGSU/bad_words.txt");

答案 2 :(得分:1)

如果您的资源已经在类路径中,则无需使用File类的相对路径。您可以从类路径轻松获取它,如:

java.net.URL url = getClass().getResource("bad_words.txt");
File file = new File(url.getPath());

甚至直接输入流:

InputStream in = getClass().getResourceAsStream("bad_words.txt");

由于您使用static方法:

InputStream in = YourClass.class.getResourceAsStream("bad_words.txt");

此外,正如我在评论中提到的那样:

p.concat("/HackGSU/bad_words.txt");

不会连接到p但返回一个新的连接字符串,因为字符串是不可变的。 只需使用:p+="/HackGSU/bad_words.txt"代替

答案 3 :(得分:0)

您可能应该使用反斜杠而不是斜杠:

p.concat("\HackGSU\bad_words.txt");

您也可以尝试输入双反斜杠:

p.concat("\\HackGSU\\bad_words.txt");