【问题标题】:out.write() in java , how to insert newlinejava中的out.write(),如何插入换行符
【发布时间】:2013-02-01 01:00:25
【问题描述】:

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

代码sn-p

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


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


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

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

value1
value2
value3 
.

【问题讨论】:

标签: java


【解决方案1】:
  • 如果 'out' 是 PrintWriter,请使用 println()。
  • 如果 'out' 是 BufferedWriter,请使用 newLine()。
  • 如果 'out' 是其他 Writer,请使用 write('\n'),或将 newLine 直接附加到您正在编写的字符串中。如果您需要系统的行分隔符,请参阅 System.getProperty() 的值为“line.separator”。

【讨论】:

  • java 是否足够聪明,可以将 \n 转换为您的操作系统(windows vs mac vs unix)的正确行终止符?
  • @Patashu 如果你使用 println() 或 newLine(),而不是 \n。
【解决方案2】:

很简单

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

就是这样。

【讨论】:

    【解决方案3】:

    使用 out.write(10);添加新行。 10 是换行符的 acsii 字符。但它不适用于 Indirect/Direct Buffer 类型的 FileChannel。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 2014-12-24
      • 2018-04-23
      相关资源
      最近更新 更多