0

Possible Duplicate:
How can a Java program use files inside the .jar for read and write?

I am working on a Java project in which I want to write to a file which is inside a jar. I know how to read it.

 URL url = getClass().getClassLoader().getResource("libs/page.properties");
 InputStream fin = url.openStream();
 if (fin != null)
 {
    ObjectInputStream objStream = new ObjectInputStream(fin);
    Object pageC = objStream.readObject();
    pageCount = Integer.parseInt(pageC.toString());
 }

Now how can I write a new value inside page.properties file?

Community
  • 1
  • 1
Shanky_Richer
  • 193
  • 1
  • 2
  • 7
  • 2
    You shouldn't be writing to a file inside a jar. – Bhesh Gurung Oct 19 '12 at 05:40
  • @BheshGurung But there is a requirement to do this. – Shanky_Richer Oct 19 '12 at 05:41
  • 1
    *"But there is a requirement to do this."* If I require pigs to fly, that will hardly make a difference to the fact they can't. – Andrew Thompson Oct 19 '12 at 05:43
  • If you want to hide files from the user (e.g. game score, don't allow editing) just make the file hidden by calling `Runtime.getRuntime().exec("attrib +H score.dat");`. For more, see [Hidding Files](http://stackoverflow.com/questions/1294989/make-a-file-folder-hidden-on-windows-with-java). – Mordechai Oct 19 '12 at 05:47
  • @AndrewThompson Sir i have tried many possible cases to do this. But i am not getting the successful case for it.This files is working as an counter for me, to avoid user-action to this file i have to put this file inside a jar. – Shanky_Richer Oct 19 '12 at 05:48
  • @AndrewThompson unless, obviously, you're Pink Floyd [Pigs on the Wing](http://en.wikipedia.org/wiki/Pigs_on_the_Wing) :) – Nishant Oct 19 '12 at 05:49
  • @Nishant Pink Floyd took a ***lot*** of psychoactive substances. ; – Andrew Thompson Oct 19 '12 at 05:50
  • *"to avoid user-action to this file i have to put this file inside a jar."* It is easy for a user to access a file inside a Jar. A Jar is a specialized Zip file. But your code writes objects apparently. They are much harder to edit. If this game is giving prize money, the details should not be stored on the client side at all, but pushed to a server (with a whole lot more security problems). – Andrew Thompson Oct 19 '12 at 05:53
  • You should really change the question to something like *"How to store game scores so user cannot easily hack them?"* What you are doing will **not** lead to an answer to that question. – Andrew Thompson Oct 19 '12 at 05:55
  • @AndrewThompson I an not working on a game project Sir. – Shanky_Richer Oct 19 '12 at 06:04

0 Answers0