DefaultStyledDocument doc = new DefaultStyledDocument();
try {
// Create some simple attributed text
SimpleAttributeSet attrs1 = new SimpleAttributeSet();
StyleConstants.setBold(attrs1, true);
StyleConstants.setItalic(attrs1, true);
doc.insertString(0, "Line 2\n", attrs1);
SimpleAttributeSet attrs2 = new SimpleAttributeSet();
StyleConstants.setForeground(attrs2, Color.black);
StyleConstants.setBackground(attrs2, Color.gray);
doc.insertString(0, "Line 1\n", attrs2);
// Write out the document
FileWriter out = new FileWriter("test.txt");
SimpleWriter sw = new SimpleWriter(out, doc);
sw.write();
out.close();
}
catch (BadLocationException ex) {ex.printStackTrace();}
catch (IOException ex) {ex.printStackTrace();}