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

yc0p9oo0  于 2021-07-05  发布在  Java
关注(0)|答案(3)|浏览(419)

使用eclipse,我的项目文件夹hackgsu中有一个名为bad_words.txt的文件。我要定位文件并读取文件的内容。我看到了如何读取文本文件相对路径的答案,我尝试了以下方法:

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)

我也回答了如何使用java?直接从internet读取文本文件,但url构造函数有问题。
如果我把本地文件路径(c://…/bad_words.txt)放进去,它就可以正常工作,但是如何让程序读取文件,这样如果我打包软件,它仍然可以找到正确的文件路径。

kb5ga3dv

kb5ga3dv1#

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

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

也可以尝试键入双反斜杠:

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

kmpatx3s2#

从错误的Angular 看,你似乎没有得到完整的路径。而不是

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

尝试

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

dgiusagp3#

如果资源已经在类路径中,则不需要使用 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" 相反

相关问题