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!

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

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

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.

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.