javaswingdomhtmleditorkit

Save HTMLDocument


package htmldocsave;

import java.io.IOException;

import javax.swing.text.BadLocationException;
import javax.swing.text.html.*;

import java.io.*;

public class HTMLDocSave 
{
    public static void main(String[] args)
    {
        HTMLDocument doc = new HTMLDocument();
        HTMLEditorKit kit = new HTMLEditorKit();

        File f = new File("greeting.html");

        try 
        {
            kit.insertHTML(doc,doc.getLength(),"<b>Hello</b>",0,0,null);
            FileOutputStream fos = new FileOutputStream(f);

            ???????????????????????????
                    fos.close();
        } 
        catch (BadLocationException | IOException e) 
        {
            e.printStackTrace();
        }

    }
}

How to save a HTML document on the file system? The javax.swing.text.html.HTMLDocument class doesn't override the toString() method and getText() removes tags.


Solution

  • Use the HTMLEditorKit.write() method.