在java中单行编辑文件

发布于 2024-10-06 09:42:19 字数 186 浏览 0 评论 0原文

我正在尝试用 Java 编辑配置文件。我真正需要做的是更改单行,因此读取整个文件并将其写回会浪费时间,因为配置文件可能很大。

有没有更有效的方法来做到这一点?除了读入/编辑/写出文件。我想将整个文件转换为字符串,替换我想要的行并将其写回。

我不知道这有多高效,有人可以给我一些其他建议吗?或者我提到的那个可以,执行时间很重要。

I'm trying to edit configuration file in Java. What I really need to do is to change single line, so reading the whole file and writing it back would be waste of time, since configuration file can be big.

Is there a more efficient way to do this? Except reading in/editing/writing out file. I thouhgt of converting entire file to string, replacing the line I want and writting it back.

I don't know how efficient would that be, can someone give me some other suggestions or the one I mentioned are ok, execution time is important.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫儰鉨ミ守葔 2024-10-13 09:42:20

我建议使用 Preferences API 相反。然后在 Windows 平台上,您的首选项将存储在注册表中。在其他平台上,使用相应的方式来保存应用程序首选项。另请参阅首选项 API 概述

I would recommend to use the Preferences API instead. Then on the Windows platform your preferences is stored in the registry. On other platforms the corresponding way to save application preferences is used. See also Preferences API Overview.

桃酥萝莉 2024-10-13 09:42:20

我们这里讨论的配置文件有多大? 1k行? 10k? 1m线?如果要编辑的行是最后一行,只需查找该行的开头,截断那里的文件并写入新的即可。如果不是……您将需要完整阅读并重新编写。

哦,你提到的两个选项实际上是相同的(读/编辑/写)。

第三,考虑到大多数配置文件的大小,我认为这是无关紧要的(除非你有奇怪的限制,比如闪存设备的写入时间太长,并且写入周期有限)。

How big of a configuration file are we talking here? 1k lines? 10k? 1m lines? If the line you want to edit is the last line, just seek to the start of the line, truncate the file there and write the new one. If it's not... you will need to read it whole and write it again.

Oh, and the 2 options you mention are actually the same (read/edit/write).

On the third hand, I think it's irrelevant (unless you have weird constraints, like a flash storage device which takes too long to write, and has limited write cycles), given the sizes of most config files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文