java Java替换文本文件中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25220340/
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
Java replace line in a text file
提问by user3879542
I found this code from another question
我从另一个问题中找到了这个代码
private void updateLine(String toUpdate, String updated) throws IOException {
BufferedReader file = new BufferedReader(new FileReader(data));
String line;
String input = "";
while ((line = file.readLine()) != null)
input += line + "\n";
input = input.replace(toUpdate, updated);
FileOutputStream os = new FileOutputStream(data);
os.write(input.getBytes());
file.close();
os.close();
}
This is my file before I replace some lines
这是我替换某些行之前的文件
example1
example2
example3
But when I replace a line, the file now looks like this
但是当我替换一行时,文件现在看起来像这样
example1example2example3
Which makes it impossible to read the file when there are a lot of lines in it.
这使得当文件中有很多行时无法读取文件。
How would I go about editing the code above to make my file look what it looked like at the start?
我将如何编辑上面的代码以使我的文件看起来像开始时的样子?
回答by Robby Cornelissen
Use System.lineSeparator()
instead of \n
.
使用System.lineSeparator()
代替\n
。
while ((line = file.readLine()) != null)
input += line + System.lineSeparator();
The issue is that on Unix systems, the line separator is \n
while on Windows systems, it's \r\n
.
问题是在 Unix 系统上,行分隔符是\n
在 Windows 系统上,它是\r\n
.
In Java versions older then Java 7, you would have to use System.getProperty("line.separator")
instead.
在 Java 7 之前的 Java 版本中,您必须System.getProperty("line.separator")
改用。
As pointed out in the comments, if you have concerns about memory usage, it would be wise to not store the entire output in a variable, but write it out line-by-line in the loop that you're using to process the input.
正如评论中指出的那样,如果您担心内存使用情况,最好不要将整个输出存储在一个变量中,而是将其逐行写在您用来处理输入的循环中.
回答by eckes
If you read and modify line by line this has the advantage, that you dont need to fit the whole file in memory. Not sure if this is possible in your case, but it is generally a good thing to aim for streaming. In your case this would in addition remove the need for concatenate the string and you don't need to select a line terminator, because you can write each single transformed line with println(). It requires to write to a different file, which is generally a good thing as it is crash safe. You would lose data if you rewrite a file and get aborted.
如果您逐行读取和修改,这具有优势,即您不需要将整个文件放入内存中。不确定这在您的情况下是否可行,但以流媒体为目标通常是一件好事。在您的情况下,这将另外消除连接字符串的需要,并且您不需要选择行终止符,因为您可以使用 println() 编写每个转换后的行。它需要写入不同的文件,这通常是一件好事,因为它是安全的。如果您重写文件并中止,您将丢失数据。
private void updateLine(String toUpdate, String updated) throws IOException {
BufferedReader file = new BufferedReader(new FileReader(data));
PrintWriter writer = new PrintWriter(new File(data+".out"), "UTF-8");
String line;
while ((line = file.readLine()) != null)
{
line = line.replace(toUpdate, updated);
writer.println(line);
}
file.close();
if (writer.checkError())
throw new IOException("cannot write");
writer.close();
}
In this case, it assumes that you need to do the replace only on complete lines, not multiple lines. I also added an explicit encoding and use a writer, as you have a string to output.
在这种情况下,它假定您只需要在完整行而不是多行上进行替换。我还添加了显式编码并使用了编写器,因为您有一个要输出的字符串。