Search code examples
javafontsfont-sizertfprintwriter

How to save font style and font size to rich text file in java


Hello I am currently working on a java text editor for a module in college. I am having some trouble getting the font styles and sizes to save to a rich text file I am currently using the printwriter method to write the text in the text area to the file. I have included my saveFile code below. This my first time posting so sorry if there's anything wrong with my post, thanks in advance.

public class saveFile  implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent e){
        //allow user to enter file name
        String s = JOptionPane.showInputDialog("Input name of file:");
        String w = "";
        try
        {
            //Allow user to enter directory
            w = JOptionPane.showInputDialog("Input Directory where you want the file:\n"
                    + "eg C:\\ for C drive");
        }
        catch(Exception writeFile)
        {
            JOptionPane.showMessageDialog(null, "Task could not be completed\nPlease try again"
                    ,"File Write Error", JOptionPane.ERROR_MESSAGE, null);
        }
        //try catch block
        try
        {
            //check if fields are null or empty
            if(!(s.isEmpty() && w.isEmpty()) && (w != null && s != null))
            {
                try
                {
                    //Create new file and append with .rtf
                    File userFile =  new File(w + s + ".rtf"); 
                    PrintWriter file = new PrintWriter(userFile);
                    //set files content = text
                    file.print(text.getText());
                    file.close();
                    File dir = new File(w);
                    if(dir.exists())
                    {
                        JOptionPane.showMessageDialog(null, "File " +  s + " created\n" + "Saved in " + w, null,JOptionPane.PLAIN_MESSAGE );
                    }
                    else
                    {
                        throw new Exception();
                    }

                }
                catch(Exception fileNotCreatedException)
                {
                    JOptionPane.showMessageDialog(null,"File not created\n"
                            + "Please check to see directory exists","File Error", 
                            JOptionPane.ERROR_MESSAGE, null);
                    fileNotCreatedException.printStackTrace();
                }

            }
        }

Solution

  • If you are working in swing with a JTextPane using content type text/rtf, then the RTFEditorKit is used for the StyledDocument.

    OutputStream out = new FileOutputStream(userFile);
    StyledDocument doc = textPane.getStyledDocument();
    StyledEditorKit editorKit = textPane.getStyledEditorKit();
    editorKit.write(out, doc, 0, doc.getLength());
    

    As the HTMLEditorKit also is StyledDocument based, it might sometimes pay to first experiment with HTML, as the HTML syntax is easier.