Pregunta

Im a novice Java programmer trying to use the HTMLEditorKit library to traverse a HTML document and alter it to my linking (mostly for the fun of it, what I'm doing could be done in hand without a problem)

But my problem is: After i have modifed my HTML file i am left with a HTMLDocument that i have no clue how to save back to a HTML file.

HTMLEditorKit kit = new HTMLEditorKit();
File file = new File("local file")
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); 
doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
InputStreamReader(url.openConnection().getInputStream()); 
FileReader HTMLReader = new FileReader(file); 
kit.read(HTMLReader, doc, 0); 

after that i do my thing with the "doc" element.

Now that im done with that i just want to save it back, preferablly overwriting the file which i got HTML from in the first place.

Anyone able to tell me how to save the modified HTMLdocument into a html file afterwards?

¿Fue útil?

Solución

You can use the write method of HTMLEditorKit class. Sample code here:

FileWriter writer = new FileWriter("local file");
try {
  kit.write(writer, doc, 0, doc.getLength());
} finally {
  writer.close();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top