在 Java 的 FileWriter 中创建一个新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18549704/
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
Create a new line in Java's FileWriter
提问by st.math
I have coded the following FileWriter
:
我编写了以下代码FileWriter
:
try {
FileWriter writer = new FileWriter(new File("file.txt"), false);
String sizeX = jTextField1.getText();
String sizeY = jTextField2.getText();
writer.write(sizeX);
writer.write(sizeY);
writer.flush();
writer.close();
} catch (IOException ex) {}
Now I want to insert a new line, just like you would do it with \n
normally, but it doesn't seem to work.
现在我想插入一个新行,就像你\n
通常做的那样,但它似乎不起作用。
What can be done to solve this?
可以做些什么来解决这个问题?
Thank you.
谢谢你。
采纳答案by Pshemo
If you want to get new linecharacters used in current OS like \r\n
for Windows, you can get them by
如果你想获得当前操作系统中使用的换行符,比如\r\n
Windows,你可以通过
System.getProperty("line.separator");
- since Java7
System.lineSeparator()
- or as mentioned by Stewart generate them via
String.format("%n");
System.getProperty("line.separator");
- 从 Java7 开始
System.lineSeparator()
- 或者正如斯图尔特所提到的那样通过
String.format("%n");
You can also use PrintStream
and its println
method which will add OS dependent line separator at the end of your string automatically
您还可以使用PrintStream
它的println
方法,它会自动在字符串的末尾添加操作系统相关的行分隔符
PrintStream fileStream = new PrintStream(new File("file.txt"));
fileStream.println("your data");
// ^^^^^^^ will add OS line separator after data
(BTW System.out
is also instance of PrintStream).
(顺便说一句System.out
,也是 PrintStream 的实例)。
回答by Stewart
回答by Stewart
回答by Prabhaker A
Try System.getProperty( "line.separator" )
尝试 System.getProperty( "line.separator" )
writer.write(System.getProperty( "line.separator" ));
回答by blackbird014
I would tackle the problem like this:
我会像这样解决这个问题:
BufferedWriter output;
output = new BufferedWriter(new FileWriter("file.txt", true));
String sizeX = jTextField1.getText();
String sizeY = jTextField2.getText();
output.append(sizeX);
output.append(sizeY);
output.newLine();
output.close();
The true in the FileWriter constructor allows to append.
The method newLine() is provided by BufferedWriter
Could be ok as solution?
FileWriter 构造函数中的 true 允许追加。
方法 newLine() 由 BufferedWriter 提供
可以作为解决方案吗?
回答by Lee Fogg
If you mean use the same code but add a new line so that when you add something to the file it will be on a new line. You can simply use BufferedWriter's newLine()
.
Here I have Improved you code also: NumberFormatException was unnecessary as nothing was being cast to a number data type, saving variables to use once also was.
如果您的意思是使用相同的代码但添加一个新行,以便在向文件中添加内容时它将位于新行上。您可以简单地使用 BufferedWriter 的newLine()
.
在这里,我还改进了您的代码:NumberFormatException 是不必要的,因为没有将任何内容转换为数字数据类型,保存变量以供使用一次也是如此。
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"));
writer.write(jTextField1.getText());
writer.write(jTextField2.getText());
writer.newLine();
writer.flush();
writer.close();
} catch (IOException ex) {
System.out.println("File could not be created");
}
回答by Roushan Kumar
Here "\n" is also working fine. But the problem here lies in the text editor(probably notepad). Try to see the output with Wordpad.
这里 "\n" 也工作正常。但这里的问题在于文本编辑器(可能是记事本)。尝试使用写字板查看输出。
回答by Nick Bell
Since 1.8, I thought this might be an additional solution worth adding to the responses:
从 1.8 开始,我认为这可能是一个值得添加到响应中的附加解决方案:
Path java.nio.file.Files.write(Path path, Iterable lines, OpenOption... options) throws IOException
路径 java.nio.file.Files.write(Path path, Iterable lines, OpenOption... options) 抛出 IOException
StringBuilder sb = new StringBuilder();
sb.append(jTextField1.getText());
sb.append(jTextField2.getText());
sb.append(System.lineSeparator());
Files.write(Paths.get("file.txt"), sb.toString().getBytes());
If appending to the same file, perhaps use an Append flag with Files.write()
如果附加到同一个文件,也许可以在 Files.write() 中使用附加标志
Files.write(Paths.get("file.txt"), sb.toString().getBytes(), StandardOpenOption.APPEND);