Solved

Text File Access inside Jar File

Posted on 2002-05-15
19
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

751 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