File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes read content of text file which is in jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "read content of text file which is in jar" Watch "read content of text file which is in jar" New topic
Author

read content of text file which is in jar

naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

hi ranchers ,

i have a jar file jkl.jar which have a text file in it named as poi.txt.

is there any way i can read and alter the content of text file poi.txt ( which is in jar)

this is attempt i have made



It prints the Special Unicode character ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

No, you can't alter resources which are stored in a jar file.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

And you're trying to read the JAR file as if it is a text file, while you should access the text file within the JAR file. You can use JarFile for that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

No, observe this code:

It assumes the jar file is in the classpath and reads the text file from that jar, assuming that all the details are got right.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

But s is "jkl.jar", not "poi.txt".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Hence why I said "assuming that all the details are got right".
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

now i have added jkl.jar in CLASSPATH and here is code

In this piece of code I have got the reference of poi.txt file which is in jar



up to now i have txtFile holding name of poi.txt file and file obj reference to it in txtFileObj

Next step is to read the poi.txt

method 1 : It throws IOException saying stream closed in Line 2



method 2 :it throws IOException FileNotFoundException



i have reference of poi.txt now what is problem here ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

You can't use java.io.File with files inside ZIP or JAR files. Instead you should use the methods of JarFile:
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

Rob Spoor wrote:You can't use java.io.File with files inside ZIP or JAR files. Instead you should use the methods of JarFile:


thanks Rob.

But how can one alter the poi.txt(in jar) there is no such method like getOutputStream() method in JarFile or ZipEntry .Is that possible, help me with a hint, if possible.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

You cannot. No program can directly edit files inside ZIP / JAR files. That's because editing one file may cause the entire structure of the ZIP / JAR file to be modified.
Some programs appear they can edit files inside ZIP / JAR files, but they actually recreate the ZIP / JAR file when they store the edited files.
naveen yadav
Ranch Hand

Joined: Oct 23, 2011
Posts: 384

here is my attempt to create a jar file problematically. i have a txt file as users.txt and want it to be bundled in a jar file called admin.jar




what details am i missing ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read content of text file which is in jar