the announced answer, which has 6 votes, is correct, with the exception of one part, it requires 1 more line of code.
Or "htmlWriter.close ();" OR "htmlWriter.flush ();" or both if you want. At the end of his code segment, because I had the same problem, and I used his version, but he lacked this part (seen from the first comment on the post: gist.github.com/4139609. So, the finished code segment :
BufferedWriter htmlWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")); System.out.println("\n" + doc.outerHtml()); htmlWriter.write(doc.toString()); htmlWriter.flush(); htmlWriter.close();
source share