• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to locate & write to files inside a Jar  RSS feed

 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a simple program that reads a string from a file. I want to pack it as a Jar so I need to locate the input file in the following way:



This code works but how can I write to a file located in the same Jar? (the getResourceAsStream returns an InputStream, not the OutputStream that I need).

Thanks in advance,


 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jars don't have files but rather resources, and you really shouldn't be trying to write into them. Instead perhaps use properties or a datafile external to the Jar.
 
Stephan van Hulst
Saloon Keeper
Posts: 7003
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I echo that you shouldn't write to a Jar file.

That said, you can. A jar file is simply a zip file with a different extension. You can edit the files in a roundabout way using java.util.zip.*.
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can't. Editing into a ZipFile is not supported, not even by the zip protocol itself. If you replace a file inside the zip file, changing the size, you will need to shift data inside the entire zip file.

Most programs that do allow changing files inside a zip file use a shadow copy - upon committing the changes the all contents are put into a new zip file; afterwards the old zip file is replaced by the new one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!