简体   繁体   English

Eclipse Java部署:如何将自动生成的文件添加到Jar文件

[英]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. 我已经完成了项目,现在将其部署到jar文件中。 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. 我的程序在Eclipse中自动生成一个文件(例如:data.sav),它出现在最前面的项目文件夹中。 When I deploy, this file doesn't contain to jar file. 部署时,此文件不包含jar文件。 So, it start with no data. 因此,它从没有数据开始。

After I run and do sth, it has made data.sav again. 我运行并执行某项操作后,它再次生成了data.sav。 But, I don't see this file in that jar file. 但是,我在那个jar文件中看不到该文件。 I say this because I hope I will overwrite my old data.sav to new data.sav. 我之所以这样说,是因为我希望将原来的data.sav覆盖为新的data.sav。

So, please teach me, how to include "data.sav" to jar file. 因此,请教我如何在jar文件中包含“ data.sav”。 I don't see this option when deploy in Eclipse. 在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. 例如 ,一个游戏的列表包含最多10个人的最高分。 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? 请参阅Java程序如何使用.jar中的文件进行读写? for more details. 更多细节。

Perhaps the high-scores would be better stored using the Preferences API. 也许可以使用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 . 将虚拟分数和任何后续的真实分数写入到user.home子目录中的文件中。 That is a place that should allow read/write to a standard Java app. 那是一个应该允许对标准Java应用程序进行读写的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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