This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Reading a text file from a jar file? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Reading a text file from a jar file?" Watch "Reading a text file from a jar file?" New topic
Author

Reading a text file from a jar file?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Hello Everybody,
Ok I think it is now the appropriate time to ask this questions after all my trials.
I have a jar file which has following types of files:
File name Directory Structure
test.class /
makePanel.class /test
pic1.gif /images/pic
pic2 /images
abc.ict /
xyz.id /images/pic
where xyz.id and abc.ict are simple text files with a change in extension. Now problem is in extracting it... As the documentation goes that I have to use ClassLoader to load same before using.. I tried the following:
For Images:

It works.
2. Now problem comes in when I want to read all the files from the directory /image/pic.. In a normal case I was trying the following code snippet...

but failed to retrieve same from jar file with the above concept.
if i tried with:

It doesn't work...perhaps the path it is giving is:
jar:file:/E:/test/test.jar!/images/pic
Can somebody help me in this regard...

regards,
arun
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
In a desperate attemp I tried with another set of codes:

Here in above the InputStream object in is null...while all other has some value..
I think I am somewhere missing the concept badly of jar files..can somebody help me...
regards,
arun
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Why can't you use JarInputStream?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Thanks Thomas for your answer and I tried with following code but again in vain...again NullPointerException...Any more idea?

regards,
Arun
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
How about showing the actual error? Where are you getting the NullPointerException?
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Oops I got yhe answer for it at last..Special thanks to Thomas who has given me a lead..
posting the code snippet if some one is interested !!



regards,
Arun
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Well done, Arun, and thanks for sharing what you learned.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
 
subject: Reading a text file from a jar file?
 
Similar Threads
Issue with ClassLoader
Problem Deploying Applet
how to load a png-file?
Current directory
images in jar files