[英]Java formatter going to new line
I am wondering is there any way after it writes to the txt file it goes to the next line ? 我想知道在写入txt文件后,有什么方法可以转到下一行吗?
stdIn2.format("%s", x[i]);
like 喜欢
a
b
c
I have tried stdIn2.format("%s\\n", x[i]);
我已经尝试过stdIn2.format("%s\\n", x[i]);
but it does't work 但这不起作用
How about: 怎么样:
stdIn2.format("%s%n", x[i]);
The %n
stands for platform-specific line separator in the formatter: %n
代表格式化程序中特定于平台的行分隔符:
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
You can use a PrintWriter
, and follow the conventions listed in the API. 您可以使用PrintWriter
,并遵循API中列出的约定。 println()
will be familiar to you immediately. println()
将立即为您熟悉。
PrintWriter out = new PrintWriter(new File("out.txt"));
out.println(String.format("%s", x[i]));
Use System.out.println(String.formtat(...))
. 使用System.out.println(String.formtat(...))
This will definitely work. 这肯定会工作。
Try using lineSeparator()
as : 尝试将lineSeparator()
用作:
stdIn2.format("%s"+System.lineSeparator(), x[i]);
This will append \\r\\n
as line separator in Windows
based environment. 这将在基于Windows
的环境中附加\\r\\n
作为行分隔符。
Using notepad.exe is not the best choice. 使用notepad.exe不是最佳选择。
In Windows a new line is defined by two chars: '\\n' and '\\r' In Linux it is only one '\\n' (In MacOs it is only '\\r' iirc) 在Windows中,新行由两个字符定义:'\\ n'和'\\ r'在Linux中仅是一个'\\ n'(在MacOs中仅是'\\ r'iirc)
Your programm only outputs a '\\n' which won't be recognised as a new line by notepad. 您的程序仅输出“ \\ n”,记事本不会将其识别为新行。 Notepad++, which is way better than notepad, will show the result as expected. 记事本++比记事本更好,它将按预期显示结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.