Saves the given Swing document content in the given file. - Java Swing

Java examples for Swing:Swing HTML

Description

Saves the given Swing document content in the given file.

Demo Code


//package com.java2s;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.text.BadLocationException;

import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class Main {
    private static final HTMLEditorKit editorKit = new HTMLEditorKit();

    /**/*from   w  w w .ja  v a2s. c  o m*/
     * Saves the given document content in the given file.
     * 
     * @param htmlFile
     *            File to save in.
     * @param doc
     *            Document containing the content.
     * @throws IOException
     * @throws BadLocationException
     */
    public static void saveDocument(File htmlFile, HTMLDocument doc)
            throws IOException, BadLocationException {
        FileOutputStream fileStream = null;
        try {
            fileStream = new FileOutputStream(htmlFile);
            editorKit.write(fileStream, doc, 0, doc.getLength());
        } finally {
            if (fileStream != null) {
                fileStream.close();
            }
        }
    }
}

Related Tutorials