Accessing a file in a jar
I'm writing a program that I am packaging into a jar. There is a data file that I would like to include in the package, that the program needs to access. In the jar, I have two folders:
src > ... > code
data > donationsList.txt
Here is the code that I have been trying to useURL urlFileName = getClass().getResource("/data/DonationsList.txt");
try
{
filePath = URLDecoder.decode(urlFileName.getFile(),"UTF-8");
}
catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
System.out.println(filePath);
It currently prints: <location of jar>/Donations Tracker.jar!/data/DonationsList.txt, which is what I thought it should be.
However, when I use this code right after the earlier parttry
{
fileIn =new FileReader(filePath);
buffFileIn =new BufferedReader(fileIn);
}
catch(FileNotFoundException fnfe)
{
System.out.println(filePath +" cannot be found");
}
Then, I get my print out that the file can't be found, and get the FileNotFoundException, and it says:
FileNotFoundException: data\DonationsList.txt (The system cannot....)
I'm thinking that because it doesn't print the whole filepath that can't be found instead of just the data\DonationsList.txt, that there may be an issue with the way I'm using the filepath, although I'm not sure. I've tried using another method opening up a JarConnection, and such. I'm just sorta outta ideas, when the method above has seemed to work for other people (as I've read on this forum)
Thanks for the help.

