Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintWriter truncates string when writing to file, why?

Tags:

java

android

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?

like image 446
Max Koretskyi Avatar asked Dec 09 '22 03:12

Max Koretskyi


2 Answers

You need to close the PrintWriter using PrintWriter#close() method

Add

out.close();
like image 125
Neeraj Jain Avatar answered Dec 26 '22 04:12

Neeraj Jain


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

like image 40
user207421 Avatar answered Dec 26 '22 04:12

user207421