• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

read content of text file which is in jar

 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 28235
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can't alter resources which are stored in a jar file.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28235
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But s is "jkl.jar", not "poi.txt".
 
Paul Clapham
Marshal
Posts: 28235
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hence why I said "assuming that all the details are got right".
 
naveen yadav
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic