简体   繁体   中英

How to read a file that is located in the project folder java

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;
}

But I received this traceback:

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)

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.

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");

Try

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

Check out the answer in this question, it might be what you are looking for!

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

If your resource is already in the classpath, you don't need to make use of relative paths with the File class. You can easily obtain it from the classpath like:

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

or even directly to an inputstream:

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

Since you have a static method use:

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

Furthermore, as I mentioned it already in my comment:

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

does not concat to p but returns a new concatenated String, because Strings are immutable. Simply use: p+="/HackGSU/bad_words.txt" instead

You probably should use backslash instead of slash:

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

You can also try to type double backslash:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM