Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java FileOutputStream中编写新行_Java_Newline_Fileoutputstream - Fatal编程技术网

如何在Java FileOutputStream中编写新行

如何在Java FileOutputStream中编写新行,java,newline,fileoutputstream,Java,Newline,Fileoutputstream,我想使用FileOutputStream编写一个新行;我尝试过以下方法,但没有一种有效: encfileout.write('\n'); encfileout.write("\n".getbytes()); encfileout.write(System.getProperty("line.separator").getBytes()); 这应该行得通。您可能忘记调用encfileout.flush() 然而,这不是书写文本的首选方式。您应该使用PrintWriter包装输出流,并享受其pri

我想使用
FileOutputStream
编写一个新行;我尝试过以下方法,但没有一种有效:

encfileout.write('\n');
encfileout.write("\n".getbytes());
encfileout.write(System.getProperty("line.separator").getBytes());

这应该行得通。您可能忘记调用
encfileout.flush()

然而,这不是书写文本的首选方式。您应该使用
PrintWriter
包装输出流,并享受其
println()
方法:

PrintWriter writer = new PrintWriter(new OutputStreamWriter(encfileout, charset));
或者,您可以从头开始使用
FileWriter
而不是
FileOutputStream

FileWriter fw = new FileWriter("myfile");
PrintWriter writer = new PrintWriter(fw);
现在就打电话

writer.println();

完成作业时,不要忘记调用
flush()
close()

这可能是查看器的问题。。。尝试在EditPlus或记事本++中打开文件。Windows记事本可能无法识别其他操作系统的换行符。您现在在哪个程序中查看文件?

要添加换行符,请使用fileOutputStream.write(10) 因为十进制值10代表ASCII中的换行符

String lineSeparator=System.getProperty(“line.separator”);
String lineSeparator = System.getProperty("line.separator");
<br>
fos.write(lineSeparator.getBytes());

fos.write(lineSeparator.getBytes());
Define“not working”(不工作)。它不是新写的line@PavanPatidar当前位置正在写新行。当然可以。尝试执行encfileout.write(“Fant.getbytes());encfileout.write(“\n”.getbytes());encfileout.write(“astic”.getbytes());如果你在一行上看到整个单词,你的问题就被确认了。特别是encfileout.write(System.getProperty(“line.separator”).getbytes();应该可以工作。@Teddy它在本地主机上工作,但当我在服务器上部署时,它不能工作。我希望您不介意我添加了缺少的字符集参数。@Pavan您能解释一下为什么它不是您想要的,所以可以修改答案/提供另一个答案吗?FileWriter和PrintWriter功能丰富。Printwriter的println()非常棒!!!请考虑用你的答案增加一些形式的解释。通常不鼓励只使用代码的答案。