[英]java write to the end of file with new line
I want to write results to the end of the file using java 我想用java将结果写到文件的末尾
FileWriter fStream;
try {
fStream = new FileWriter("recallPresision.txt", true);
fStream.append("queryID=" + queryID + " " + "recall=" + recall + " Pres=" + presision);
fStream.append("\n");
fStream.flush();
fStream.close();
} catch (IOException ex) {
Logger.getLogger(query.class.getName()).log(Level.SEVERE, null, ex);
}
I put "\\n"
in the statement , it writes to the file but not with new line 我把"\\n"
放在语句中,它写入文件但不是新行
I want to print results with new line 我想用新行打印结果
The newline sequence is system dependent. 换行序列取决于系统。 On some systems its \\n
, on others it's \\n\\r
, \\r\\n
, \\r
or something else entirely different. 在某些系统上它\\n
,在其他系统上它是\\n\\r
, \\r\\n
, \\r
\\r\\n
或其他完全不同的东西。 Luckily, Java has a built in property which allows you to access it: 幸运的是,Java有一个内置属性,允许您访问它:
String newline = System.getProperty("line.separator");
fStream.append("\n");
// don't guess the line separator!
fStream.append(System.getProperty("line.separator"));
It does print the newline, what you want is a blank line at the end. 它确实打印换行符,你想要的是最后一个空白行。 Add another \\n
. 添加另一个\\n
。
Try using \\r\\n
instead. 请尝试使用\\r\\n
而不是。
Also, you should find that if you open your text file in a rich-text-editor, such as wordpad, your append has actually worked. 此外,您应该会发现,如果您在富文本编辑器(如wordpad)中打开文本文件,那么您的追加实际上是有效的。
Edit: Ignore me. 编辑:忽略我。 Jeffery and Andrew's answers are much better. 杰弗瑞和安德鲁的答案要好得多。
You could also change to: 你也可以改为:
fStream = new FileWriter("recallPresision.txt", true);
PrintWriter out = new PrintWriter(fStream);
out.println("queryID=" + queryID + " " + "recall=" + recall + " Pres=" + presision);
out.flush();
out.close();
fStream.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.