简体   繁体   中英

Eclipse Java Deployment: how to add auto-generate file to Jar file

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM