Java:FileWriter:创建一个新行 [英] Java: FileWriter: Create a new line
本文介绍了Java:FileWriter:创建一个新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了一个小文件编写器:
I've made a little file writer:
try {
FileWriter writer = new FileWriter(new File("file.txt"), false);
try {
String sizeX = jTextField1.getText();
String sizeY = jTextField2.getText();
writer.write(sizeX);
writer.write(sizeY);
writer.flush();
writer.close();
} catch (NumberFormatException e) {
}
} catch (IOException ex) {
}
现在我想创建一个新行,就像你通常可以使用\ n一样,但它似乎不起作用......
Now I want to make a new line, just like you can do it with "\n" normally, but it doesn't seem to work...
有谁知道怎么做这么新的一行?
Does anyone know how to make such a new line?
提前致谢! :)
推荐答案
如果你想获得当前操作系统中使用的新行字符,如 \\\\ n
对于Windows,你可以通过
If you want to get new line characters used in current OS like \r\n
for Windows, you can get them by
-
System.getProperty(line.separator);
- ,因为Java7
System.lineSeparator()
- 或如Stewart所述,通过
String.format(%n)生成它们;
System.getProperty("line.separator");
- since Java7
System.lineSeparator()
- or as mentioned by Stewart generate them via
String.format("%n");
您还可以使用 PrintStream
及其 println
自动在字符串末尾添加OS依赖行分隔符的方法
You can also use PrintStream
and its println
method which will add OS dependent line separator at the end of your string automatically
PrintStream fileStream = new PrintStream(new File("file.txt"));
fileStream.println("your data");
// ^^^^^^^ will add OS line separator after data
( BTW System.out
也是PrintStream的实例。
(BTW System.out
is also instance of PrintStream).
这篇关于Java:FileWriter:创建一个新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文