用Java替换文本文件的第一行

时间:2023-01-21 22:21:49

I have a text file where I want to change only the first line of the file. The file could be millions of rows long, so I'd rather not have to loop over everything, so I'm wondering if there is another way to do this.

我有一个文本文件,我只想更改文件的第一行。该文件可能是数百万行,所以我宁愿不必遍历所有内容,所以我想知道是否还有其他方法可以做到这一点。

I'd also like to apply some rules to the first line so that I replace instances of certain words with other words.

我还想在第一行应用一些规则,以便用其他单词替换某些单词的实例。

Is this possible?

这可能吗?

5 个解决方案

#1


16  

A RandomAccessFile will do the trick, unless the length of the resulting line is different from the length of the original line.

除非结果行的长度与原始行的长度不同,否则RandomAccessFile将执行此操作。

If it turns out you are forced to perform a copy (where the first line is replaced and the rest of the data shall be copied as-is), I suggest using a BufferedReader and BufferedWriter. First use BufferedReader's readLine() to read the first line. Modify it and write it to the BufferedWriter. Then use a char[] array to perform a brute-force copy of the remainder of the file. This will be more efficient than doing the copy line by line. Let me know if you need details..

如果事实证明你*执行一个副本(其中第一行被替换,其余数据将按原样复制),我建议使用BufferedReader和BufferedWriter。首先使用BufferedReader的readLine()来读取第一行。修改它并将其写入BufferedWriter。然后使用char []数组执行文件其余部分的暴力复制。这比逐行复制更有效。如果您需要详细信息,请告诉我。

Another option is to perform the reading and writing inside the same file. It'll be a bit more complex though. :) Let me know if you need details on this as well..

另一种选择是在同一文件中执行读写。但它会有点复杂。 :)如果您需要有关详细信息,请告诉我..

#2


4  

If the new line has a different amount of characters (bytes) than the original first line, you will have to re-write the whole file to get rid of the gap or avoid overwriting part of the second line.

如果新行具有与原始第一行不同的字符数(字节),则必须重写整个文件以消除间隙或避免覆盖第二行的一部分。

Of course, various tools like String.replaceFirst(String regex, String replacement) (javadoc) or the RandomAccessFile (javadoc) can help you with this task.

当然,String.replaceFirst(String regex,String replacement)(javadoc)或RandomAccessFile(javadoc)等各种工具可以帮助您完成此任务。

#3


2  

You want a RandomAccesssFile. Using the file you can read and write wherever you want in the file.

你想要一个RandomAccesssFile。使用该文件,您可以在文件中的任何位置读取和写入。

It is much like an InputStream and OutputStream, but it allows reading and writing wherever you like.

它很像InputStream和OutputStream,但它允许在任何你喜欢的地方读写。

#4


1  

apply a regex only once. String.replaceFirst("regex", "replacementstring") : http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceFirst(java.lang.String,%20java.lang.String)

只应用一次正则表达式。 String.replaceFirst(“regex”,“replacementstring”):http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceFirst(java.lang.String,% 20java.lang.String)

Open the file as RandomAccessFile. Read the 1st line into a string and then apply the change and then write the string back.

将文件作为RandomAccessFile打开。将第1行读入字符串然后应用更改,然后将字符串写回。

#5


-5  

Why not write a Perl script and invoke it using Runtime.exec(). Not a pure java solution though. Also have a look at this article before going deep http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

为什么不编写Perl脚本并使用Runtime.exec()调用它。虽然不是纯粹的java解决方案。深入了解本文之前http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

#1


16  

A RandomAccessFile will do the trick, unless the length of the resulting line is different from the length of the original line.

除非结果行的长度与原始行的长度不同,否则RandomAccessFile将执行此操作。

If it turns out you are forced to perform a copy (where the first line is replaced and the rest of the data shall be copied as-is), I suggest using a BufferedReader and BufferedWriter. First use BufferedReader's readLine() to read the first line. Modify it and write it to the BufferedWriter. Then use a char[] array to perform a brute-force copy of the remainder of the file. This will be more efficient than doing the copy line by line. Let me know if you need details..

如果事实证明你*执行一个副本(其中第一行被替换,其余数据将按原样复制),我建议使用BufferedReader和BufferedWriter。首先使用BufferedReader的readLine()来读取第一行。修改它并将其写入BufferedWriter。然后使用char []数组执行文件其余部分的暴力复制。这比逐行复制更有效。如果您需要详细信息,请告诉我。

Another option is to perform the reading and writing inside the same file. It'll be a bit more complex though. :) Let me know if you need details on this as well..

另一种选择是在同一文件中执行读写。但它会有点复杂。 :)如果您需要有关详细信息,请告诉我..

#2


4  

If the new line has a different amount of characters (bytes) than the original first line, you will have to re-write the whole file to get rid of the gap or avoid overwriting part of the second line.

如果新行具有与原始第一行不同的字符数(字节),则必须重写整个文件以消除间隙或避免覆盖第二行的一部分。

Of course, various tools like String.replaceFirst(String regex, String replacement) (javadoc) or the RandomAccessFile (javadoc) can help you with this task.

当然,String.replaceFirst(String regex,String replacement)(javadoc)或RandomAccessFile(javadoc)等各种工具可以帮助您完成此任务。

#3


2  

You want a RandomAccesssFile. Using the file you can read and write wherever you want in the file.

你想要一个RandomAccesssFile。使用该文件,您可以在文件中的任何位置读取和写入。

It is much like an InputStream and OutputStream, but it allows reading and writing wherever you like.

它很像InputStream和OutputStream,但它允许在任何你喜欢的地方读写。

#4


1  

apply a regex only once. String.replaceFirst("regex", "replacementstring") : http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceFirst(java.lang.String,%20java.lang.String)

只应用一次正则表达式。 String.replaceFirst(“regex”,“replacementstring”):http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceFirst(java.lang.String,% 20java.lang.String)

Open the file as RandomAccessFile. Read the 1st line into a string and then apply the change and then write the string back.

将文件作为RandomAccessFile打开。将第1行读入字符串然后应用更改,然后将字符串写回。

#5


-5  

Why not write a Perl script and invoke it using Runtime.exec(). Not a pure java solution though. Also have a look at this article before going deep http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

为什么不编写Perl脚本并使用Runtime.exec()调用它。虽然不是纯粹的java解决方案。深入了解本文之前http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html