Java中如何写入文本文件
我正在用java做一个项目,我需要添加和修改我的 运行时的文本文件,该文件分组在 jar 中。
我正在使用 class.getResourceAsStream(filename) 这个方法我们 可以从类路径读取该文件。
我想写入同一个文本文件。
对此可能的解决方案是什么。 如果我无法更新 jar 中的文本文件还有什么其他解决方案? 感谢任何帮助。
I am doing a project in java and in that i need to add and modify my
text file at runtime,which is grouped in the jar.
I am using class.getResourceAsStream(filename) this method we
can read that file from class path.
i want to write into the same textfile.
What is the possible solution for this.
If i can't update the text file in jar what other solution is there?
Appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里最简单的解决方案是不将文件放入 jar 中。听起来您正在将文件放入 jar 中,以便您的用户只需要担心一个包含与该程序相关的所有内容的文件。这是人为的限制,只会增加麻烦。
有一个简单的解决方案,仍然允许您仅分发 jar 文件。启动时,尝试从文件系统读取文件。如果找不到它,请使用程序中编码的默认值。然后,当进行更改时,您可以将其写入文件系统。
The easiest solution here is to not put the file in the jar. It sounds like you are putting files in your jar so that your user only needs to worry about one file that contains everything related to that program. This is an artificial constraint and just add headaches.
There is a simple solution that still allows you to distribute just the jar file. At start up, attempt to read the file from the file system. If you don't find it, use default values that are encoded in you program. Then when changes are made, you can write it to the file system.
通常,您无法更新使用
getResourceAsStream
找到的文件。它可能是 JAR/ZIP 文件中的一个文件……写入它需要重写整个 JAR 文件。它可能是由 Url 类加载器提供的远程文件。为了您的理智(和良好实践),您不应尝试更新通过类路径访问的文件。如果需要,请从 JAR 文件(或其他文件)中读取文件,将其复制到常规文件系统中,然后更新副本。
我并不是说在所有情况下都不可能这样做。事实上,在大多数正常情况下,您可以通过一些努力来做到这一点。但是,这不受支持,并且没有标准 API 可以执行此操作。
此外,尝试更新资源可能会导致类加载器出现异常。例如,我希望 JAR 文件中的资源在应用程序重新启动之前不会更新(从应用程序的角度来看)。但是分解的 JAR 文件中的资源可能会更新......尽管新资源可能不会显示。
最后,在某些情况下更新资源是不可能的:
当用户没有应用程序安装目录的写访问权限时。对于正确管理的 UNIX/Linux 计算机来说,这是典型的情况。
当从远程服务器获取 JAR 文件时,您可能无法写回更新。
当您使用任意自定义类加载器时,您无法知道更新资源的实际字节应存储在哪里,也无法存储它们。
In general, you can't update a file that you located using
getResourceAsStream
. It might be a file in a JAR/ZIP file ... and writing it would entail rewriting the entire JAR file. It might be a remote file served up by a Url classloader.For your sanity (and good practice), you should not attempt to update files that you access via the classpath. If you need to, read the file out of the JAR file (or whatever), copy it into the regular file system, and then update the copy.
I'm not saying that it is impossible to do this in all cases. Indeed, in most normal cases you can do it with some effort. However, this is not supported, and there are no standard APIs for doing this.
Furthermore, attempts to update resources are liable to cause anomalies in the classloader. For example, I'd expect resources in JAR files to not update (from the perspective of the application) until the application restarted. But resources in exploded JAR files probably would update ... though new resources might not show up.
Finally, there are cases where updating a resource is impossible:
When the user doesn't have write access to the application's installation directory. This is typical for a properly administered UNIX / Linux machine.
When the JAR file is fetched from a remote server, you are likely not to be able to write the updates back.
When you are using an arbitrary custom classloader, you've got no way of knowing where the actual bytes of an updated resource should be stored, and no way of storing them.
Java 中的所有 JAR 重写技术看起来都很相似。打开 Jar 文件,读取其所有内容,然后编写一个包含未修改内容(以及您希望进行的修改)的新 Jar 文件。对于类路径上的 Jar 文件,这种技术并不可取,更不用说您正在运行的 Jar 文件了。
如果您决定必须这样做,Java World 有几篇文章:
修改档案,第 1 部分
修改档案,第 2 部分
All JAR rewriting techniques in Java look similar. Open the Jar file, read all of it's contents, and write a new Jar file containing the unmodified contents (and the modifications you whished to make). Such techniques are not advisable for a Jar file on the class path, much less a Jar file you're running from.
If you decide you must do it this way, Java World has a few articles:
Modifying Archives, Part 1
Modifying Archives, Part 2
避免将项目放入 Jar 文件的一个好解决方案是从用户主目录中的隐藏子目录中读取属性文件(如果存在)。逻辑看起来有点像这样:
查看 java.util.Properties,并记住它们有两种不同的加载和存储格式(基于键 = 值和基于 XML)。选择最适合您的一款。
A good solution that avoids the need to put your items into a Jar file is to read (if present) a properties file out of a hidden subdirectory in the user's home directory. The logic looks a bit like this:
Look to java.util.Properties, and keep in mind that they have two different load and store formats (key = value based and XML based). Pick the one that suits you best.
将信息存储在以下任意位置:
PersistenceService
存储信息。这是我的 演示。PersistenceService
的。另外,如果您的用户同意使用受信任的小程序(这似乎有点过分),您可以将信息写入
user.home
的子目录。Store the information in any of:
PersistenceService
to store the information. Here is my demo. of thePersistenceService
.Also, if your users will agree to a trusted applet (which seems overkill for this), you might write the information to a sub-directory of
user.home
.