简体   繁体   English

java printWrite不向文件写入任何内容,但创建文件

[英]java printWrite writes nothing to file, but file is created

i use this to write string to file. 我用它来写字符串到文件。 I see this file has been created by this code but no content inside. 我看到此文件已由此代码创建,但内部没有内容。

PrintWriter out = new PrintWriter("/home/paul/Documents/linuxwork/core_java/Welcome/src/output.txt");
out.println("helloworld");

Did I miss anything? 我有想念吗?

THanks 谢谢

Possibly your missing close() 可能是您缺少的close()

PrintWriter out = new PrintWriter("/home/paul/Documents/linuxwork/core_java/Welcome/src/output.txt");
out.println("helloworld");
out.close();//<-- added this

Automatic line flushing is disabled with the constructor you used , so you need to call out.flush() to flush the stream to the file: 您使用的构造函数禁用了自动行刷新功能,因此您需要调用out.flush()将流刷新到文件:

PrintWriter out = new PrintWriter("/home/paul/Documents/linuxwork/core_java/Welcome/src/output.txt");
out.println("helloworld");
out.flush();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM