PrintWriter的书面文件,为什么当截断字符串? [英] PrintWriter truncates string when writing to file, why?
本文介绍了PrintWriter的书面文件,为什么当截断字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的PrintWriter这样写入文件:
GSON G =新GSON();
文章文章= g.fromJson(读卡器,Article.class);
JSON字符串= g.toJson(条);为PrintWriter了=的新PrintWriter(新的FileWriter(cacheFile.getAbsolutePath()));
的out.print(JSON);
然而,当我看到它在文件中的JSON字符串被截断。可能是什么原因呢?
解决方案
您需要使用的的PrintWriter#close()方法
添加
out.close();
I'm writing to a file using PrintWriter like this:
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. What could be the reason for that?
解决方案
You need to close the PrintWriter using PrintWriter#close() method
Add
out.close();
这篇关于PrintWriter的书面文件,为什么当截断字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文