Solved

Text File Access inside Jar File

Posted on 2002-05-15
19
274 Views
Last Modified: 2007-12-19
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?
0
Comment
Question by:ericpastoor
  • 10
  • 8
19 Comments
 
LVL 7

Expert Comment

by:yoren
ID: 7011513
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 "/".
0
 

Author Comment

by:ericpastoor
ID: 7011532
sometimes i have to make new directories as well, is this possible to do inside the jar file?
0
 

Author Comment

by:ericpastoor
ID: 7011538
also i am using FileInputStreams, instead of inputStreams.
0
 
LVL 7

Expert Comment

by:yoren
ID: 7011599
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")
0
 

Author Comment

by:ericpastoor
ID: 7011636
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.
0
 

Author Comment

by:ericpastoor
ID: 7011664
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!
0
 
LVL 7

Expert Comment

by:yoren
ID: 7011673
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.
0
 
LVL 7

Expert Comment

by:yoren
ID: 7011692
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.
0
 

Author Comment

by:ericpastoor
ID: 7011714
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?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Expert Comment

by:yoren
ID: 7011824
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
0
 

Author Comment

by:ericpastoor
ID: 7011836
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?
0
 
LVL 4

Expert Comment

by:kylar
ID: 7012166
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
0
 

Author Comment

by:ericpastoor
ID: 7012221
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.
0
 
LVL 7

Expert Comment

by:yoren
ID: 7012716
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.
0
 

Author Comment

by:ericpastoor
ID: 7013649
What is the alternative then if i dont use a jar file? How else can i package up an application and deliver it?
0
 
LVL 7

Accepted Solution

by:
yoren earned 50 total points
ID: 7014339
You can deliver the application in a jar, but it sounds like you may need to do a setup program which will extract the read/write files and your classes into a user-specified directory. You can write a simple setup program yourself (in Java of course) or use a package like InstallShield or InstallAnywhere.

Here's what your shipping jar might look like:

myapp_setup.jar:
  classes/ [your java classes]
  files/ [your read/write files]
  Setup.class [extracts everything to a user-selected dir]
  META-INF/MANIFEST.MF [points to Setup as the main class]
0
 

Author Comment

by:ericpastoor
ID: 7014347
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
0
 
LVL 7

Expert Comment

by:yoren
ID: 7014405
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.
0
 

Author Comment

by:ericpastoor
ID: 7014423
thanks for your help!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now