0

I have a .txt file and a image in a file called res. I added in the file to my path as well. I did the code bellow and it works in my Eclipse IDE just fine. When exporting a jar and running it, it does nothing. Running the jar with cmd says the class path can not be found. SO I tried the second chunk of code with no success. My image that is there works fine. bgi = new ImageIcon(getClass().getResource("bg.png"));

Scanner s = null;
        try {
            s = new Scanner(new File("res//10kaddress.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ArrayList<String> paddress = new ArrayList<String>();
        while (s.hasNext()){
            paddress.add(s.next());
        }
        s.close();

So I tried doing this below and no mater what I do it will not read the txt file

URL url = GUI.class.getResource("10kaddress.txt");
    File ff = new File(url.getPath());


    Scanner s = null;
    try {
        s = new Scanner(ff);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    ArrayList<String> paddress = new ArrayList<String>();
    while (s.hasNext()){
        paddress.add(s.next());
    }
    s.close();

and get this error

java.io.FileNotFoundException: C:\Users\Major%20Lee\Sketch\GUI\res\10kaddress.txt (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.util.Scanner.<init>(Unknown Source)
    at GUI.main(GUI.java:68)
Exception in thread "main" java.lang.NullPointerException
    at GUI.main(GUI.java:73)

Any help would be great. Thanks!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SolidCloudinc
  • 329
  • 10
  • 27
  • Well obvious question, is the file at the path: C:\Users\Major%20Lee\Sketch\GUI\res\10kaddress.txt? – davecom Dec 01 '13 at 06:26
  • Get the resource As stream and construct the Scanner using it. http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29 – Jayan Dec 01 '13 at 06:27
  • possible duplicate: http://stackoverflow.com/questions/403256/how-do-i-read-a-resource-file-from-a-java-jar-file – Amir Afghani Dec 01 '13 at 06:30

2 Answers2

1

I would open it as a ResourceStream (or I would just use a ResourceBundle), that is like this -

InputStream is = getClass().getResourceAsStream("10kaddress.txt");
Scanner s = new Scanner(is);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

When you have a file embedded in a JAR, you need to use the getResourceAsStream(), as it has been pointed out.

Also, I suspect that there is a problem in your path. From the error I can see that you are on a Windows machine. Your path is :

res//10kaddress.txt  

When escaping the separators, use a / (single forward slash) or a \\( double backslash). The backslash has another backslash with it and is hence called as being escaped. Try changing the path and see if that works for you.

So, make your path as:

res/10kaddress.txt   

Double-check manually to see if the file is at the path specified.

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • 2
    Nice answer. +1 I generally avoid mentioning `getResourceAsStream` because it typically returns a non-repositionable stream, while a number of methods that work with streams require them to be repositionable (most notably when dealing with media files). So I stick to using `getResource(..)` to get an URL. The stream returned by [`URL.openStream()`](http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#openStream%28%29) **is** repositionable. – Andrew Thompson Dec 01 '13 at 07:10