java java中的out.write(),如何插入换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14638015/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:55:32  来源:igfitidea点击:

out.write() in java , how to insert newline

java

提问by Rahul Kumar

When writing to a text file in java , how do I enter values into a new line

在 java 中写入文本文件时,如何在新行中输入值

code snippet

代码片段

while (rs.next()) {
                int sport = rs.getInt("sport");


                String name = rs.getString("name");


                out.write(sport + " : " + name);}

the text file populates " value1 value2 value3...etc" I want it to populate

文本文件填充“ value1 value2 value3 ... etc”我希望它填充

value1
value2
value3 
.

回答by user207421

  • If 'out' is a PrintWriter, use println().
  • If 'out' is a BufferedWriter, use newLine().
  • If 'out' is some other Writer, use write('\n'), or append the newLine directly to the string you're writing. If you want the system's line separator, see System.getProperty() with the value "line.separator".
  • 如果 'out' 是 PrintWriter,则使用 println()。
  • 如果 'out' 是 BufferedWriter,则使用 newLine()。
  • 如果 'out' 是其他 Writer,请使用 write('\n'),或将 newLine 直接附加到您正在编写的字符串中。如果您需要系统的行分隔符,请参阅 System.getProperty(),其值为“line.separator”。

回答by zeyorama

Very simple

很简单的

out.write(sport + " : " + name + "\n");

out.write(sport + " : " + name + "\n");

That's all.

就这样。