问题描述
我需要将数据存储到 .jar 文件中的文件中并再次读取.
I need to store data into files inside .jar file and read it again.
我知道我可以使用 Class.getResourceAsStream()
方法,但它返回一个我可以读取的InputStream
.但我正在寻找一种写作的方式.
I know that I can use Class.getResourceAsStream()
method but it returns an InputStream
that I can read from. But I look for a way to write.
推荐答案
不,你没有.
而是将默认"文件存储在 Jar 中.如果更改,请将更改后的文件存储在其他位置.一个常见的地方是 user.home
的子目录.检查文件时,首先检查文件系统中是否存在被修改的文件,如果不存在则加载默认文件.
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 中" 是一种策略,而 在运行之间保存首选项" 可能是目标.
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.
相关: 什么是XY问题?
这篇关于应用程序如何使用 JAR 中的文件进行读写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!