0

I have finished my project, and now I have deployed it to jar file. But I have met a problem:

My program auto generate a file (For example: data.sav) in Eclipse, it appears at the foremost project folder. When I deploy, this file doesn't contain to jar file. So, it start with no data.

After I run and do sth, it has made data.sav again. But, I don't see this file in that jar file. I say this because I hope I will overwrite my old data.sav to new data.sav.

So, please teach me, how to include "data.sav" to jar file. I don't see this option when deploy in Eclipse.

Here is the code I want to write file:

ObjectOutputStream writer = new ObjectOutputStream(
    new FileOutputStream("data.sav"));

For more clearer why I do that. For example, a game has a list that contain most 10 people highscore. when you first play, no people play,yet, so I must "cheat" by write some people in this highscore list, so my program has some "dummny player" that has played before.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
hqt
  • 29,632
  • 51
  • 171
  • 250
  • 1
    *"My program auto generate a file (For example: data.sav)" I don't get it. Your program apparently does this at 'run-time' when the app. is already in a Jar. It can't write anything to the Jar at that time. BTW - leave noise like 'Thanks :)' out of posts. – Andrew Thompson Apr 13 '12 at 08:38
  • You want to change the content of the jar file?! – alexvetter Apr 13 '12 at 08:42
  • I have edited again. Please teach me – hqt Apr 13 '12 at 08:44
  • 1
    *"Please teach me"* 1. Learn that people don't get informed of comments unless you add @PersonName before the comment. ;) – Andrew Thompson Apr 13 '12 at 08:56

1 Answers1

1

Jars are read-only. See How can a Java program use files inside the .jar for read and write? for more details.

Perhaps the high-scores would be better stored using the Preferences API. It takes care of 'where to put the information' automatically.

Failing that, I would have the app. write the dummy scores and any subsequent real scores into a file in a sub-directory of user.home. That is a place that should allow read/write to a standard Java app.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • For example, I have a folder /src/. Can you tell me how to write to this folder,please. I just can write to absolutely path. When I use: `File file = new File(Model.class.getResource("/src/data.sav").toString());` error happen – hqt Apr 13 '12 at 09:20
  • You have either not read or not understood my message and linked answer. I'm not sure how to proceed. – Andrew Thompson Apr 13 '12 at 09:35