简体   繁体   中英

PrintWriter truncates string when writing to file, why?

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();

It doesn't. You failed to close the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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