在 Java 中替换文本文件的第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/202148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace first line of a text file in Java
提问by Bialecki
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?
这可能吗?
采纳答案by volley
A RandomAccessFile
will do the trick, unless the length of the resulting line is different from the length of the original line.
ARandomAccessFile
可以解决问题,除非结果行的长度与原始行的长度不同。
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..
如果事实证明您被迫执行复制(其中第一行被替换,其余数据应按原样复制),我建议使用 aBufferedReader
和BufferedWriter
。首先使用BufferedReader
'sreadLine()
读取第一行。修改它并将其写入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..
另一种选择是在同一个文件中执行读写操作。不过会复杂一些。:) 如果您还需要有关此的详细信息,请告诉我..
回答by jjnguy
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,但它允许在您喜欢的任何地方进行读取和写入。
回答by anjanb
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 打开。将第一行读入一个字符串,然后应用更改,然后将字符串写回。
回答by David Schmitt
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)这样的各种工具可以帮助您完成这项任务。
回答by questzen
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