问题描述
我有一个包含新行的字符串。我将此字符串发送到函数以将String写入文本文件:
public static void writeResult(String writeFileName,String文本)
{
try
{
FileWriter fileWriter = new FileWriter(writeFileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(text);
//始终关闭文件。
bufferedWriter.close();
}
catch(IOException ex){
System.out.println(写入文件时出错'+ writeFileName +');}
} //结束writeResult函数
但是当我打开文件时,我发现没有任何新行。
当我在控制台屏幕中显示文本时,它会以新行显示。如何在文本文件中编写换行符。
编辑:
假设这是参数 text
我发送到上面的函数:
我从城市返回了大约三个o那个
的时钟可能在下午非常厌恶生活。
我在旧国家已经三个月了,是
如何写这个字符串因为它(在新行中)在文本文件中。我的函数将字符串写在一行中。你能为我提供一种方法来将文字写入文件,包括换行吗?
编辑2:
文字是最初是在.txt文件中。我使用以下方式阅读文本:
while((line = bufferedReader.readLine())!= null)
{
sb.append(line); //将行附加到字符串
sb.append('\ n'); //追加新行
} //结束时
其中 sb
是一个StringBuffer
在编辑2:
while((line = bufferedReader.readLine())!= null)
{
sb.append(line ); //将行附加到字符串
sb.append('\ n'); //追加新行
} //结束而
您正在阅读文本文件,并附加换行符。不要追加换行符,换行符不会在一些简单的Windows编辑器(如记事本)中显示换行符。而是使用以下方法附加特定于操作系统的行分隔符字符串:
sb.append(System.lineSeparator());
( for Java 1.7 and 1.8 )
或
sb.append(System.getProperty(line.separator)) );
( Java 1.6及以下)
或者,稍后您可以使用 String .replaceAll()
用String特定的换行符替换StringBuffer中构建的字符串中的\ n
:
String updatedText = text.replaceAll(\ n,System.lineSeparator())
但是在构建字符串时附加它会更有效率,而不是追加'\ n'
并在以后替换它。 / p>
最后,作为开发人员,如果您使用记事本查看或编辑文件,您应该删除它,因为有更多功能强大的工具,如,或您最喜欢的Java IDE。
I have a string that contains new lines. I send this string to a function to write the String to a text file as:
public static void writeResult(String writeFileName, String text)
{
try
{
FileWriter fileWriter = new FileWriter(writeFileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(text);
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println("Error writing to file '"+ writeFileName + "'");}
} //end writeResult function
But when I open the file, I find it without any new lines.When I display the text in the console screen, it is displayed with new lines. How can I write the new line character in the text file.
EDIT:Assume this is the argument text
that I sent to the function above:
I returned from the city about three o'clock on that
may afternoon pretty well disgusted with life.
I had been three months in the old country, and was
How to write this string as it is (with new lines) in the text file. My function write the string in one line. Can you provide me with a way to write the text to the file including new lines ?
EDIT 2:The text is originally in a .txt file. I read the text using:
while((line = bufferedReader.readLine()) != null)
{
sb.append(line); //append the lines to the string
sb.append('\n'); //append new line
} //end while
where sb
is a StringBuffer
In EDIT 2:
while((line = bufferedReader.readLine()) != null)
{
sb.append(line); //append the lines to the string
sb.append('\n'); //append new line
} //end while
you are reading the text file, and appending a newline to it. Don't append newline, which will not show a newline in some simple-minded Windows editors like Notepad. Instead append the OS-specific line separator string using:
sb.append(System.lineSeparator());
(for Java 1.7 and 1.8)orsb.append(System.getProperty("line.separator"));
(Java 1.6 and below)
Alternatively, later you can use String.replaceAll()
to replace "\n"
in the string built in the StringBuffer with the OS-specific newline character:
String updatedText = text.replaceAll("\n", System.lineSeparator())
but it would be more efficient to append it while you are building the string, than append '\n'
and replace it later.
Finally, as a developer, if you are using notepad for viewing or editing files, you should drop it, as there are far more capable tools like Notepad++, or your favorite Java IDE.
这篇关于如何在Java中将新行字符写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!