PrintWriter无法打印出完整的字符串 [英] PrintWriter not printing out complete string
问题描述
我有以下代码
FileWriter F = new FileWriter("out.txt");
PrintWriter H = new PrintWriter(F);
H.print(split[split.length - 2]);
H.print("END");
但是,当我检查txt时,最后一个文本不是'END',而是字符串中单词的一部分.是"repa"
When I examine the txt however, the last text is NOT 'END', but part of a word in the string. It is "repa"
当我这样做
FileWriter F = new FileWriter("out.txt");
PrintWriter H = new PrintWriter(F);
System.out.print(split[split.length - 2]);
我得到的最后一点文字是数字'49'-这是正确的.
The last bit of text I get is the number '49' - this is correct.
看来PrintWriter没有完全写出字符串.但是,当我这样做
It appears that the PrintWriter is not fully writing out the string. However, when I do this
FileWriter F = new FileWriter("out.txt");
PrintWriter H = new PrintWriter(F);
H.print(split[split.length - 2]);
H.println(pdfInText)://Another string
H.print("END");
原始"文本实际上已经完成了-这是什么?
The 'original' text now actually finishes - what is this?
推荐答案
是否关闭PrintWriter
?从其Javadoc
Do you close the PrintWriter
? From its javadoc
与PrintStream类不同,如果启用了自动刷新,则仅在调用println()方法之一时才执行此操作,而不是在输出换行符时才执行. println()方法使用平台自己的行分隔符概念,而不是换行符.
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println() methods is invoked, rather than whenever a newline character happens to be output. The println() methods use the platform's own notion of line separator rather than the newline character.
如果您确实关闭了PrintWriter
(而不是FileWriter!),请在打印后尝试使用flush()
,以强制执行该操作.
If you do close the PrintWriter
(and not the FileWriter!) try a flush()
after printing, to force that.
这篇关于PrintWriter无法打印出完整的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!