使用java来修改文件内容 [英] Use java to modify file contents in place

查看:129
本文介绍了使用java来修改文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要修改就地文件的特定内容。

not 要创建新文件并重写旧文件。此外,文件很小,只有几MB,每个最大

对于那些想知道(虽然我不知道这是否与OP有关),我需要修改文件是版本控制的一部分,需要修改只读版本。
这是可能的Java apis?

如果不是有一个库提供这个?


Java允许随机访问和写入磁盘上的文件。但是,写入文件中间只能覆盖字节 - 即用其他字节替换特定字节 - 而不能将数据插入到文件中间。为此,您必须在插入点之后重写所有内容。把文件想象成一个恰好驻留在磁盘上的字符数组( char [] )。随机访问允许你做相当于

$ $ $ $ codehar $] file = ... //磁盘上的文件
char [] newData = ... //要写入的数据
int pos = ... //文件中要写入
的位置(i = 0; i {
file [pos + i] = newData [i];



$ b 要将数据插入到文件中,需要将数据插入数组的过程相同。插入点之后的所有数据将不得不移动到右侧以容纳插入的数据。如果你用一个 short 字符串替换(即删除字节),那么编辑后的数据将不得不左移。其次,你说:


我需要修改作为版本控制一部分的文件,并且需要修改一个只读版本


只读就是这个意思。如果该文件是只读的,则不能以任何方式对其进行修改,无论是否使用随机访问。



您还在评论中说:


新文件不会在源代码管理下。我将不得不找到一种方法来添加它。我试图避免这个


如果文件在源代码管理下,那么很可能是在本地副本上工作。只要更新的文件与原始文件具有相同的名称,并且位于相同的目录中,则创建该文件的新实例应该没有什么区别。您只需将更新后的版本提交给源代码管理系统即可。



但是,如果要更新源代码管理系统库中的文件 >那么你可能会永久损坏系统。

I need to modify specific contents of a file in-place.
I do not want to create a new file and rewrite the old. Also the files are small just a couple MB each the max.
For those wondering (although I am not sure if this is related to the OP), I need to modify files that are part of version control and need to modify a read-only version. It is much simpler to do the modification in place.
Is this possible with Java apis?
If not is there a library that offers this?

解决方案

Java allows random access and writing to files on disk. However, writing to the middle of a file can only over-write bytes -- i.e. replace specific bytes with other bytes -- and cannot insert data into the middle of a file. To do that you must rewrite everything after the insertion point. Think of the file as an array of characters (char[]) that happens to reside on a disk. Random-access allows you to do the equivalent of

char[] file    = ... // the file on disk
char[] newData = ... // the data to be written
int pos = ...        // the position in the file to write to
for (i=0; i<newData.; i++) 
{ 
    file[pos+i] = newData[i];
}

To insert data in a file would require the same process as inserting data into an array. All data after the point of insertion would have to be moved to the right to accommodate the inserted data. If you are replacing with a shorter string (i.e. removing bytes) then the data after the edit would have to be shifted left.

Secondly, you state:

I need to modify files that are part of version control and need to modify a read-only version

Read-only means exactly that. If the file is read-only, you cannot modify it in any way, whether you use random access or not.

You also said in a comment:

The new file will not be under source control.I will have to find a way to add it.I am trying to avoid this

If the file is under source control, you are most likely working on a local copy. As long as the updated file has the same name as the original and is in the same directory, it should make no difference if you create a new instance of the file. You merely have to commit the updated version to the source control system.

If, however, you are updating the file within the source control system's repository then you are likely to permanently damage the system.

这篇关于使用java来修改文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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