如何用Java写入文本文件 [英] how to write into a text file in Java

查看:98
本文介绍了如何用Java写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中做一个项目,我需要在运行时添加和修改我的
文本文件,该文件在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.

我正在使用class.getResourceAsStream(filename)这个方法我们
可以从类路径读取该文件。

I am using class.getResourceAsStream(filename) this method we can read that file from class path.

我想写入相同的文本文件。

i want to write into the same textfile.

这可能是什么解决方案。
如果我无法更新jar中的文本文件,还有其他解决方案吗?
感谢任何帮助。

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.

推荐答案

这里最简单的解决方案是不将文件放入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.

有一个简单的解决方案仍然允许您只分发jar文件。在启动时,尝试从文件系统中读取文件。如果找不到,请使用在程序中编码的默认值。然后,当进行更改时,您可以将其写入文件系统。

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.

这篇关于如何用Java写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆