简体   繁体   English

PrintWriter在写入文件时截断字符串,为什么?

[英]PrintWriter truncates string when writing to file, why?

I'm writing to a file using PrintWriter like this: 我正在使用PrintWriter写一个文件,如下所示:

Gson g = new Gson();
Article article = g.fromJson(reader, Article.class);
String json = g.toJson(article);

PrintWriter out = new PrintWriter(new FileWriter(cacheFile.getAbsolutePath()));
out.print(json);

However, the string in json is truncated when I see it in the file. 但是,当我在文件中看到它时,json中的字符串被截断。 What could be the reason for that? 可能是什么原因?

You need to close the PrintWriter using PrintWriter#close() method 您需要使用PrintWriter #close()方法关闭PrintWriter

Add

out.close();

It doesn't. 它没有。 You failed to close the file. 您无法关闭该文件。

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

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