java 应用程序如何使用 JAR 中的文件进行读写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5052311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can an app use files inside the JAR for read and write?
提问by Moataz Aahmed Mohammed
I need to store data into files inside .jar file and read it again.
我需要将数据存储到 .jar 文件中的文件中并再次读取。
I know that I can use Class.getResourceAsStream()
method but it returns an InputStream
that I can readfrom. But I look for a way to write.
我知道我可以使用Class.getResourceAsStream()
方法,但它返回一个InputStream
我可以读取的。但我正在寻找一种写作方式。
回答by Andrew Thompson
I need to store data into files inside .jar file and read it again
我需要将数据存储到 .jar 文件中的文件中并再次读取
No you don't.
不,你没有。
Instead store the 'default' file inside the Jar. If it is changed, store the altered file in another place. One common place is a sub-directory of user.home
. When checking for the file, first check the existence of an altered file on the file system, and if it does not exist, load the default file.
而是将“默认”文件存储在 Jar 中。如果更改,请将更改后的文件存储在其他位置。一个常见的地方是user.home
. 检查文件时,首先检查文件系统上是否存在被修改的文件,如果不存在,则加载默认文件。
Note that it is generally better to describe the goal, rather than the strategy. 'Store changed file in Jar'is a strategy, whereas 'Save preferences between runs'might be the goal.
请注意,通常最好描述目标而不是策略。 “在 Jar 中存储更改的文件”是一种策略,而“在运行之间保存首选项”可能是目标。
Related:What is the XY problem?
相关:什么是 XY 问题?
回答by Vance Maverick
This is not really supported. In principle, you could operate on the jar file, but there's no guarantee the new contents would be correctly loaded. Let your build tools manage the jar file -- and choose something else for persistent storage managed by your program itself. Like a file.
这不是真的支持。原则上,您可以对 jar 文件进行操作,但不能保证新内容会正确加载。让您的构建工具管理 jar 文件——并为由您的程序本身管理的持久存储选择其他内容。就像一个文件。
回答by Peter Lawrey
It is unlikely that you can change a loaded jar safely. Changing it while it is being used is not a good idea.
您不太可能安全地更换已加载的 jar。在使用时更改它不是一个好主意。
If you want to do this, use a plain file system directory and add/remove files from it. Even this may not work as you expect.
如果要执行此操作,请使用普通文件系统目录并从中添加/删除文件。即使这可能不会像您期望的那样工作。
回答by OrangeDog
You can manipulate any jar file using the package java.util.jar
(or indeed just java.util.zip
). As files inside a jar will be compressed, this isn't the most timeefficient way for you to store data.
您可以使用包java.util.jar
(或实际上只是java.util.zip
)操作任何 jar 文件。由于 jar 中的文件将被压缩,因此这不是您存储数据的最省时的方式。
You should probably use a directory somewhere else (e.g. System.getProperty("user.home") + "/.myProgram"
) or see java.util.prefs
.
您可能应该在其他地方使用目录(例如System.getProperty("user.home") + "/.myProgram"
)或查看java.util.prefs
。
回答by Binaek Sarkar
Class.getResource()
returns a URL. Theoretically, you can use this URL to create your InputStream
and OutputStream
. But in most cases, the generated JAR is a read-only file (or archive). So your application might trip when trying to use it.
Class.getResource()
返回一个 URL。理论上,您可以使用此 URL 来创建您的InputStream
和OutputStream
. 但在大多数情况下,生成的 JAR 是只读文件(或存档)。因此,您的应用程序在尝试使用时可能会跳闸。