This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read content of text file which is in jar

 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't alter resources which are stored in a jar file.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But s is "jkl.jar", not "poi.txt".
 
Paul Clapham
Sheriff
Pie
Posts: 20763
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hence why I said "assuming that all the details are got right".
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic