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!