[英]Printwriter creates empty files even after using printwriter.close
I am using PrintWriter
but it sometimes creates empty files. 我正在使用
PrintWriter
但有时会创建空文件。
I have also used PrintWriter.close()
at the end but it still creates empty file sometimes. 最后我也使用了
PrintWriter.close()
,但有时它仍会创建一个空文件。
Code: 码:
StringBuilder fileContent = new StringBuilder();
for (Iterator<String> i = fieldMap.values().iterator(); i.hasNext();)
{
String fieldValue = i.next();
LOG.debug("Write information to outputfile: fieldValue" + fieldValue);
fileContent.append(fieldValue);
if (i.hasNext())
fileContent.append("~");
}
// Member errorMessage is only set in case of fatal error and might
// be appended without risk since in
// this case this content is only written to logger.
fileContent.append(errorMessage);
fileContent.append("\n");
PrintWriter docFileWriter = new PrintWriter(fileName, "UTF-8");
docFileWriter.write(fileContent.toString());
docFileWriter.close();
Be sure, your fileName
is valid and your fileContent
is not empty (debbuging with condition or add console-outputs). 确保您的
fileName
有效,而fileContent
不为空(使用条件进行调试或添加控制台输出)。
To test the PrintWriter
you can use this short example code: 要测试
PrintWriter
,可以使用以下简短示例代码:
PrintWriter docFileWriter = new PrintWriter("C:/temp/temp.txt", "UTF-8");
docFileWriter.write("Some special text: Aäç/)!é");
docFileWriter.close();
When your files are sometimes empty but exists, you should keep attention on the fileContent
variable. 当文件有时为空但存在时,应注意
fileContent
变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.