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.

[2122 byte] By [macman104a] at [2007-11-26 16:06:59]
# 1
If you want to read the file with a BufferedReader, I think this will work.1. Use the URL method openStream() to get an InputStream.2. Use the InputStream to construct an InputStreamReader.3. Use the InputStreamReader to construct a BufferedReader.
atmguya at 2007-7-8 22:29:11 > top of Java-index,Desktop,Deploying...
# 2

Awesome, now I got that to work, by using getResourceAsStream, unfortunately there doesn't seem to be a corresponding way to get an output stream to the file. I read the file in at the beginning of the session and then write back out to the file on close. I've got the stream all set up, but I can't seem to grab an output stream to the file.

macman104a at 2007-7-8 22:29:11 > top of Java-index,Desktop,Deploying...
# 3
Java does not provide a means to directly update a jar. Unjar the jar into a directory, replace the old file with the updated version, and rejar the directory contents.There may be something already written that does this, I don't know. Search and see what you find.
ChuckBinga at 2007-7-8 22:29:11 > top of Java-index,Desktop,Deploying...
# 4
Ah, well thanks for that. At least I can stop trying different variations of outputstreams now, heh.
macman104a at 2007-7-8 22:29:11 > top of Java-index,Desktop,Deploying...