0

When we want to load a static file e.g. a picture, a sound file, a file containing information about a game map,... we can store them as resources in jar file and use getClass.getResource("images/splash.png") (also getResourceAsStream) to load and use them. But when we want to read and write into a file like settings file, I don't think using resources is a good way, because i think resources are designed to store read/only files that are not supposed to change, like splash screen image or a game's background music; These are my reasons to think this way:

  1. That is why return value of getResourceAsStream is an instance of InputStream and we don't have a similar function which gives us an OutputStream, because we're not supposed to alter resource files.
  2. Writing into resources changes program .jar file and i guess it's not a good thing at all; Because if we do so: we can't use check-sums to verify file, if we are a limited user and system administrator doesn't give us write permission we can't make changes into main .jar file, user-specific preferences are hard or impossible to implement,...

So, my questions are:

  1. Which parts of my thoughts and assumptions are right or wrong?
  2. If they're right what is the best(I mean short and portable between OSs and Computers) way to store files like that? (Application setting/preferences, A game save file, ...)

(@Some user who may wants to mark this as duplicate: I don't think my question is a duplicate, i searched in the site, I admit it has some common parts with some questions but it's not duplicate!)

4

1 回答 1

4

您在上面 #2 中的三个观察结果是不将设置存储在资源文件中的正当理由,无论提供何种 API。

在 Java 中有多种保存设置的方法,包括:

  • Java 系统属性“ user.home”提供用户的主目录,用户应该对它有写访问权。您可以在其下创建一个特定于应用程序的子目录。
  • Java 提供了一个Preferences API。这可以将设置存储在目录中或(在 Windows 上)在注册表中。
  • OSGI 提供了一个首选项 API
  • 如果您使用的是 Eclipse RCP,您可以使用 ConfigurationScope 写入配置目录。请参阅 Eclipse 常见问题解答“什么是首选项范围”)。
于 2013-07-05T13:52:33.673 回答