如何读取位于项目文件夹Java中的文件 [英] How to read a file that is located in the project folder java

查看:71
本文介绍了如何读取位于项目文件夹Java中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用eclipse,我在名为HackGSU的项目文件夹中有一个名为bad_words.txt的文件.我想找到文件并读取文件的内容.我看到了这个答案如何读取文本文件的相对路径,我尝试了这个:

Using eclipse, I have a file titled bad_words.txt in my project folder titled HackGSU. I want locate the file and read the contents of the file. I saw this answer how to read text file relative path and I tried this:

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构造函数存在问题.

I also so this answer How to read a text file directly from Internet using Java?, but I had a problem with the URL constructor.

如果我放置本地文件路径(C://.../bad_words.txt),它可以正常工作,但是如何获取程序以读取文件,以便如果我打包软件,它仍然可以找到正确的文件路径.

If I put the local file path (C://.../bad_words.txt) it works just fine, but how can I get the program to read the file so that if I package the software it will still find the proper file path.

推荐答案

看着错误看起来好像您没有获得完整的路径.代替

Looking at the error it doesn't look like you are getting the full path. Instead of

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

尝试

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

这篇关于如何读取位于项目文件夹Java中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆