This content has been marked as final. Show 9 replies
You can use Java's JarFile and JarEntry classes to read the contents of a JAR file, then use JarOutputStream to create a new one.
So the process is to read the content first, filter out things which need modification ( the xml file), then build your entry based on what you need ( the modified xml file and the other entries i.e classes, etc..)
Sure, you can!
A jar file is Java version of zip files,
You would first extract the contents of the jar file, make updates to your xml files and again create a new jar according to your need,
You would use jar utility that comes with Java SDK for this,
See [how to use Jar tool.|http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/jar.html]
but that wouldn't update the files in the jar as it would create a new jar :)
Updating files inside an archive without unpacking them and later repacking that archive is indeed not simple (though theoretically possible).
You'd have to update the binary content of the archive, all the while keeping the structure of the archive as well as its checksums and indices correct. Some dedicated tools can achieve that, most don't bother (I think Winzip for example can do it to a degree, it can replace files in archives with new versions).
If what you are trying to do is have a program persist configuration changes (which seems the most common reason to want to do this) you'll probably be better off using the Preferrences class. This allows you to store preferences without worrying about where they live (on Windows they are stored in the registry).
malcolmmc wrote:Next you'll want me to write a fully-qualified class name on a slip of paper, work it into a fortune cookie, and pass that to you at the end of your meal. Shhh!
But if you do use a file, it seems like a good idea to me to put the file path in Preferences, so you aren't committed to a specific location.