开发者

Save File that is in source package

开发者 https://www.devze.com 2023-01-24 03:01 出处:网络
I\'m trying to save file directories so that when my app opens again the directories are remembered. I want to save the directories to a file called \'directories.txt\' which is in my source code pack

I'm trying to save file directories so that when my app opens again the directories are remembered. I want to save the directories to a file called 'directories.txt' which is in my source code package.开发者_JS百科

I did learn how to read from it by using:

getClass().getResourcesAsStream("/getresources/directories.txt")

So my question is how do I write to that file? Is this possible.

Thanks,

nt


It's not possible. The file you are getting is a resource in the class path of your application, like a .class file. And like a class file it cannot be rewriten into its original place (being either a jar, zip or binaries .class folder). It's read only and cannot be used for persistence.

To persist any data you are going to have to use an external resource. You can write to an actual file in any path in the file system. That file is going to be external to your class path files.


  1. Saving user data in the application's installation directory is fundamentally wrong! There can be multiple users on a machine, even at the same time. And they may not (should not, in fact) have write permission on application files. The "there is one user, and he owns the whole machine" mental model became wrong around 10 years ago even on Windows (and about 50 years ago elsewhere).

  2. Use the Java Preferences API instead of inventing your own mechanism.


It is possible under certain circumstances: see this SO answer. However, it is complicated and I'd strongly recommend that you don't try it.

There are various OS specific and Java standard / preferred ways of storing user preference information. Use them, and don't be tempted to hack the JAR file.

0

精彩评论

暂无评论...
验证码 换一张
取 消