简体   繁体   English

Java-覆盖文件的特定部分

[英]Java - overwriting specific parts of a file

I would like to update specific part of a text file using Java. 我想使用Java更新文本文件的特定部分。 I would like to be able to scan through the file and select specific lines to be updated, a bit like in a database, for instance given the file: 我希望能够浏览文件并选择要更新的特定行,有点像数据库中的行,例如给定文件:

ID Value
1  100
2  500
4  20

I would like to insert 3 and update 4, eg 我想插入3并更新4,例如

ID Value
1  100
2  500
3  80
4  1000

Is there a way to achieve this (seemingly) easy task? 有没有办法实现这个(看似)简单的任务? I know you can append to a file, but I am more interested in a random access 我知道您可以附加到文件,但是我对随机访问更感兴趣

Load the file into memory, change your value, and then re-write the file 将文件加载到内存中,更改您的值,然后重新写入文件

if there's a way to insert into a file without loading it, I haven't heard of it. 如果有一种方法可以在不加载文件的情况下将其插入文件,则我从未听说过。 You have to move the other data out of the way first. 您必须先将其他数据移开。

unless you're dealing with huge files, frequently, performance isn't too much of a concern 除非您经常处理大文件,否则性能并不是太大的问题

I know you can append to a file, but I am more interested in a random access 我知道您可以附加到文件,但是我对随机访问更感兴趣

You're trying to insert and delete bytes in the middle of a file. 您正在尝试在文件中间插入和删除字节。 You can't do that. 你不能那样做。 File systems simply don't (in general) support that. 文件系统根本不支持(通常)。 You can overwrite specific bytes, but you can't insert or delete them. 您可以覆盖特定的字节,但不能插入或删除它们。

You could update specific records with random access if your records were fixed-length (in bytes) but it looks like that's not the case. 如果您的记录是固定长度(以字节为单位),则可以使用随机访问来更新特定记录,但事实并非如此。

You could either load the whole file into memory, or read from the original file, writing to a new file with either the old data or the new data as appropriate on a per line basis. 您既可以将整个文件加载到内存中,也可以从原始文件中读取,然后每行将旧数据或新数据写入新文件中。

You can do so using Random Access files in java where you can place your current write and read position using available methods. 您可以使用Java中的随机访问文件来执行此操作,在其中可以使用可用方法放置当前的读写位置。 you can explore more here 你可以在这里探索更多

As said by the previous answers, it's not possible to do that symply using streams. 正如前面的答案所言,不可能使用流来做到这一点。 You could try to use properties, that are key, value pairs that can be saved and modified in a text file. 您可以尝试使用可以在文本文件中保存和修改的属性(即键,值对)。 For example you can add to a file a new property with the command 例如,您可以使用以下命令将新属性添加到文件中

setProperty(String key, String value)

This method adds a new property or, if already existing, modifies the value of the property with the choosen key. 此方法添加一个新属性,或者如果已经存在,则使用selectedn键修改该属性的值。

Obviously, new properties are added at the end of the file but the lack of ordering is not a problem for performances because the access to the file is made with the getProperty method that calls the Hashtable method put. 显然,新属性被添加到文件的末尾,但是缺少排序对于性能而言不是问题,因为对文件的访问是通过调用Hashtable方法的getProperty方法进行的。

See this tutorial for some examples: 有关示例,请参见本教程:

http://docs.oracle.com/javase/tutorial/essential/environment/properties.html http://docs.oracle.com/javase/tutorial/essential/environment/properties.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM