我从另一个问题中找到了这段代码。
但是当我替换一行时,文件现在看起来像这样。
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();
}
这是我在替换某些行之前的文件。
example1
example2
example3
但是当我替换一行时,文件现在看起来像这样。
example1example2example3
当文件中有很多行时,这使得阅读该文件变得不可能。
如何编辑上面的代码,使得我的文件看起来像最初那样?
\r\n
作为换行符。 - Jens