1

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.

Community
  • 1
  • 1
Nathaniel Brown
  • 61
  • 2
  • 4
  • 11

3 Answers3

2

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");
JohnG
  • 9,259
  • 2
  • 20
  • 29
1

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

Mad Matts
  • 1,118
  • 10
  • 14
0

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");
  • `concat` only won't change a thing in the outcome, because Strings are immutable, you need a new assignment back to p – Mad Matts Oct 22 '16 at 12:51