954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading in text file from outside JAR file

Hi all,

I'm trying read in the contents of a series of text files located in a folder in the same directory as a jar file. I've been trying to work out how to do this for a while now, but to no success, so any pointers as to where I'm going wrong would be greatly appreciated.

I'm using two methods, one to get the names of the text files in the folder (called "resources"), and another method to read the contents of these files.

(I'm using InputStreamReader to read the files as I need to specify the UTF-8 encoding because these files contain both Latin and Japanese characters which need to be displayed in a GUI.)

Here's the code with the try/catches omitted:

private void setupTextFiles()
{
    String path = "resources/";
    File folder = new File( path );
    File[] fileList = folder.listFiles();

    for( File file : fileList )
    {
        String filename = file.getName();

        if( filename.endsWith( ".txt" ) || filename.endsWith( ".TXT" ) )
        {
            readTextFile( filename );
        }
    }
}

private void readTextFile( String filename )
{
    String path = "resources/" + filename;
    InputStream is = getClass().getResourceAsStream( path );
    InputStreamReader isr = new InputStreamReader( is, "UTF-8" );
    BufferedReader br = new BufferedReader( isr );
    String line = br.readLine();

    while( line != null )
    {
        // process lines of text

        line = br.readLine();
    }

    br.close();
    isr.close();
    is.close();
}


However, I'm getting a "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" which tracks back to line 22 in the above code, so clearly it can't find the folder/path being specified.

Anyway, any help/advice/pointers about this or a better way to go about this would be a big help, thanks!

jiraiya
Light Poster
29 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Is the value of the variable: is null?
Print out the value of path to see what file it is looking for and check if that file exists.

Are the files on the classpath?
Check the API doc for the method getResourceAsStream

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

I don't think you can use getResource... for things outside the jar/classpath (hopefully someone will correct me if I'm wrong...).

However, this little snippet from deep in my old code box will give you a File reference to the jar that the class in which you execute this code came from...

File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());

then from that File object you can extract the containing directory (getParentFile()) and use that to make the full path to your text files

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Thanks James. That's a helpful bit of code!

So it seems that I have to use a File object to get to a text file that's located outside of the Jar file, but then this gives me encoding problems as the files are all UTF-8 encoded and File uses the default system encoding which is Cp1252 in this case. And the only way to get around the encoding problem is to use an InputStream object, but I can't use this to get to files located outside of the Jar file. And then I'm stuck, erg.

jiraiya
Light Poster
29 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Yeah, I can't remember now where I found the getProtectionDomain bit - that's the most non-obvious thing, but I always stash stuff like that away in case it comes in useful later. I use it to display the jar file creation date when there are problems - a kind of automatic version number check.
Let me know when you get the whole thing working?
Cheers
J

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

Okay it works fine now. I just swapped the offending line...

InputStream is = getClass().getResourceAsStream( path );


...for this line...

FileInputStream is = new FileInputStream( filePath );


...and I can get to files outside the jar file and set the encoding fine. Phew.

jiraiya
Light Poster
29 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Mark this thread "solved" so people know there's a solution here.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: