| Author |
read content of text file which is in jar
|
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
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: 16487
|
|
|
No, you can't alter resources which are stored in a jar file.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
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
How To Ask Questions How To Answer Questions
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
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: 19232
|
|
|
But s is "jkl.jar", not "poi.txt".
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
Hence why I said "assuming that all the details are got right".
|
 |
naveen yadav
Ranch Hand
Joined: Oct 23, 2011
Posts: 380
|
|
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: 19232
|
|
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: 380
|
|
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: 19232
|
|
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: 380
|
|
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 ?
|
 |
 |
|
|
subject: read content of text file which is in jar
|
|
|