Link to home
Start Free TrialLog in
Avatar of ericpastoor
ericpastoor

asked on

Text File Access inside Jar File

I want to read and write to some text files that i want to include inside my jar file rather than outside of it.  This is a java application, so im not worried about applets, but i just want to get to the files inside of the jar file, so they are invisible to the end user. Is this possible? If so, how do i do it?
Avatar of yoren
yoren

Sure it's possible. Try:

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

Note that the package name will be prepended to the filename. If you don't want that to happen, prepend a "/".
Avatar of ericpastoor

ASKER

sometimes i have to make new directories as well, is this possible to do inside the jar file?
also i am using FileInputStreams, instead of inputStreams.
I don't think you can use a FileInputStream from within a JAR. You can get a URL with getResource(), but that's about it.

New directories are fine. It defaults to the directory of the class name (unless you prepend with "/"). You can access other directories as well:

...getResourceAsStream("subdir/file.txt")
great thanks...i just tried one file and it worked beautifully reading the file in.
I switched from FileInputStream to InputStream very easily.
How do i do output? Right now i use a fileWriter and a bufferedWriter.
also, in one instance i do this

        File dateFile = new File("Database/statistics.txt");

all i do with this file is dateFile.lastModified(); to check the date of the file.
is there a way to do this also if this file is in the jar file? Since i dont use an inputstream, i wasnt sure. Thanks!
You mean output back to the same JAR? I'm not sure that's possible. You can easily write a new JAR, though, using the java.util.zip and java.util.jar packages.
If you need to get modification times, then the getResource() technique won't do it for you. Check out java.util.zip. JARs are really just ZIP files with a manifest, so you can use all the ZipFile and ZipEntry methods on them.
Maybe im confusing you on what im trying to do, or maybe im confused now.
What i want to do is give the user a jar file with some database files inside the jar file. WHen they run the program, it will open up these database files. Then they will use the program which will update the database.  Then i want their updates to get written back into the jar file, so that next time they run the program all the files they previously changed are up to date.

Make sense?
Ah. In that case, I'd suggest that your program:

1. Extracts all the files to a temporary directory, using java.util.zip.*

2. Does the database stuff which modify the temp. files

3. Creates a new zip/jar from the temp files

4. Remove the old zip/jar and the temp files
Can it create a jar file titled the same thing as itself though, and then remove itself like you are saying in step 3 and 4?
Another option is to do this:

1) create the new files that you want to write (in memory or on disk)

2) Create a zip/input and output streams.

3) read the manifest and update it with the new files.

4) using the PipedInput and PipedOutput streams, write everything from the old file into the new file

5) add in the extra files

6) remove the old jar and rename the new.

This prevents you from having to write all the files in the jar to disk and then read them and write them again. Disk access is pretty slow, so this would be a faster method.

Cheers,
Kylar
Is this that strange of a question that this is really the best way to access and write a file? I think its strange that no one else out there gives someone a jar file with files that need to be manipulated, yet they dont want the user to see these files. THere has got be an easier way than all of this copying files.
Jar files are usually used only for reading, not writing. So yes, this is uncommon. The limitation you're hitting is with the zip/jar format; you can't update files within the archive. Instead, you have to create a new archive and copy the old files to the new files.

The bottom line is that you will need to read the old archive and write a new one. You can do this in memory, like kylar suggested, or on disk, but you still have to do it. I recommend writing the new archive to a different file before removing the old one. Otherwise, you could lose the archive if the system crashes during the operation.
What is the alternative then if i dont use a jar file? How else can i package up an application and deliver it?
ASKER CERTIFIED SOLUTION
Avatar of yoren
yoren

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Do you have any links to examples for creating this setup program in java. Id rather write something myself than rely on someone elses software, especially when it would probably come at cost. thanks for your help
I don't have any examples for you, but ZeroG gives away their simple version of InstallAnywhere (called NOW) for free. See http://www.zerog.com.
thanks for your help!