How to save  JTextPane contents to a file ?

Hello,
I'd like to save the content of a JTextPane document into a buffer then to save it into a Database Column (I'm using MYSQL).
If I'm using the getText() method, all attributes (color, font, and so forth) are lost
I have also tried to use the write(Writer) method into a ByteOutputStream, but here again, attributes are lost.
Thanks to anyone who may provide me with an example.
Gege

While the RTFEditorKit allows you to save files all with the same styling, the HTMLDocument class (if set as your JTextPane's StyledDocument) and HTMLEditorKit (works for both default StyledDocuments (on a more limited basis, I believe) and HTMLDocument's) allow you to save heterogeneous styling. However, it does NOT support the StyleConstants.ComponentAttribute, and does not provide a satisfactory exception when you attempt to use them (at least last time I checked - attempting to insert a component into an HTMLDocument yielded a NullPointerException, which wasn't useful at the time, but now that Java's gone open source or whatever, it's much more useful...).
So anyway, the point is, you can probably just use your regular old JTextPane combined with HTMLEditorKit.write() and HTMLEditorKit.read() methods. You will need to translate components into some sort of text (if they're Serializable, just write them to a ByteArrayOutputStream and convert it into a String, add some custom escape-char-like magic, and you're there) before using the write method (and of course, translating it back after you read it in).
Hope this helps.

Similar Messages

  • How to save the contents of a file(not a text file)

    Hi all, I want to save the contents of a file(,png file, not a text file) as a field in a class. Shall I have it as a string or byte array or something?
    I have tried saving a file in a string but there were some problems with loading the file from a different platform. Following is my code.
            String string;
            try {
                StringBuffer sb = new StringBuffer(1024);
                char[] characterArray = new char[1024];
                BufferedReader br = new BufferedReader(new FileReader(file));
                while(br.read(characterArray) != -1){
                    sb.append(String.valueOf(characterArray));
                br.close();
                string= sb.toString();
            } catch (IOException ex) {
                ex.printStackTrace();
            }and I use the following code to recover the string back to a stream, and save that stream back to a time later.
        ByteArrayInputStream bais = new ByteArrayInputStream(map.getBytes());I realized that because this file is not a text file, the string could cause some problems. But anyone could tell me if I should use a byte array or someting? and how?
    Any help would be appreciated!
    Cheers,
    Jing

    You should use a byte array, and the binary streams (InputStream a& OutputStream). Never use Strings and Reader/Writer if you have binary data.
    Kaj

  • How to save the contents of one file into another file?

    Hai,
    i'm trying to save the contents of an existing file into a new file...
    for example.. if i'm having a ms word file namely ss.doc..
    now i want to save itz contents into another file namele dd.doc..
    How shall i do it..
    Can an one plzz explain me...
    senthil.

    Hi, Senthil.
    This Forum is not a general discussion forum.
    You don't believe that the InDesign SDK is a general purpose API. Do you?
    I think you must post issues like this where they belong, in this case in a Microsoft Word Forum.
    Best regards.
    Oscar.

  • How to store the contents of a file

    Hi,
    I'm using forms6i and database 10g.
    Through forms if a user selects a filename , and clicks a button or something,
    the contents of the file should be saved in the database.
    The file can be of any type, like .doc,.pdf,.xml,.html etc...
    and the contents filed will be of type varchar
    Please help me do this..
    Thanks

    Do you really want to save the "Content" of a file or the file itself? If you try to save the contents of a .doc or .pdf in a column with a VARCHAR2 datatype, you are going to corrupt the contents of the file since these file types have binary data in them as well as text. I think you would have greater success storing the actual file in a BLOB column.
    Here are a few Oracle Support documents that discuss how to store and retrieve files stored as BLOBs in the database.
    Doc ID: 168277.1 - How to Upload Binary Documents Back to Database BLOB Column from Forms
    Doc ID: 330146.1 - How to write BLOBs Stored Inside the Database Out to Files.
    Hope this helps,
    Craig B-)
    If someone's response is helpful or correct, please mark it accordingly.

  • How to save Android contacts as Excel file on Mac computer?

    I want to manage and edit my contacts on my cell phone, my friend suggested me save them as Excel format! So now i am trying this way, but i can't find the right method to do this! Do you know how to save Android contacts as Excel file on Mac computer?

    If you want to save Android Phone contacts to Excel format, you can have a try the following method:
    Step 1. Connect Your Android Phone to Computer
    After downloading and installing the Coolmuster Android Assistant software on your computer, launch it and you will get the program interface prompts you to connect your phone to the computer. Just use a USB cable to connect your phone to the computer and the program will detect the connected phone automatically. If it is the first time to run this software, you may encounter the following picture and be required to enable USB debugging on your phone at first. If your device can be detected by the program, you can directly skip to the next step.
    Step 2: Go to the Contacts windows
    All your phone data are categorized on the top menu. To transfer Android phone's contacts, you can go to click the "Contacts" icon to enter the Contacts window. Navigate to the left panel and click on the "All Contacts" option, then, all the contacts in your phone will show in list on the right window. Mark the contacts you want to backup on your computer and click the button of "Backup".
    Step 3. Start to export contacts to computer at once
    Once you click on the "Backup" button, there will be a "Path" dialog appears, asking you to choose an output location where you want to save the exported contacts. Specify an output folder and then click the "Ok" button. Then, all your phone contacts will be exported to the output location immediately, with all the email address, company's name, home address, and more contacts info perfectly kept in the output CSV or XLS file.

  • How to save my emails and adressbook files from an old ibook?

    How to save my emails and adressbook files from an old ibook (bought around 2002) ?
    The screen from the old ibook doesn´t work anymore (logicboard problem I guess).
    I saved everything else with starting it in target modus. That worked fine.
    I want to delete the files after that as well, because I wanna give away the old ibook and don´t want anyone to read my emails!
    Little helpers I have:
    A running MacBook
    External Screen (and cables to connect)
    Fire Wire Cable

    Don't know how helpful this will be but I often copy mail from one computer to another, I drag the mail from the mail window to the other computers drop box, then from the other computer double click it and mail asks me what I want to do with it, of course without a screen you'd need to use back to my mac.
    If you don't have back to my mac running, you could set up a second user account on the newer mac, import everything via target mode as suggested by c above, and then drag the mail to the first user account drop box.

  • PDF saves as content of last file sent

    Everytime I save a word 365 doc into a pdf it turns out to be the contents of the last pdf with this name (but date in doc file name changed). I even restarted my pc & updated it, but it doesn't help.
    I have windows 8.1, Microsoft 365, internet explorer, and a pc.

    Now it wont even open something I just downloaded.It I update it will it change my toolbar or anything on my pc?Thank you.Maureen
           From: sukritd15 <[email protected]>
    To: MAUREEN SMITH <[email protected]>
    Sent: Friday, February 20, 2015 2:24 AM
    Subject:  PDF saves as content of last file sent
    PDF saves as content of last file sent
    created by sukritd15 in Creating, Editing & Exporting PDFs - View the full discussionHi Maureen , Is it happening with all the word 365 doc? Which version of Acrobat are you using?If it is not the latest version ,try updating it to the latest version ,i.e Acrobat 11.0.10Launch Acrobat>Navigate to Help>Check for Updates I believe the PDF producer is Acrobat and not Microsoft. Please share the document with us so that we can check at our end. RegardsSukrit Dhingra If the reply above answers your question, please take a moment to mark this answer as correct by visiting: https://forums.adobe.com/message/7213183#7213183 and clicking ‘Correct’ below the answer Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: Please note that the Adobe Forums do not accept email attachments. If you want to embed an image in your message please visit the thread in the forum and click the camera icon: https://forums.adobe.com/message/7213183#7213183 To unsubscribe from this thread, please visit the message page at , click "Following" at the top right, & "Stop Following"  Start a new discussion in Creating, Editing & Exporting PDFs by email or at Adobe Community For more information about maintaining your forum email notifications please go to https://forums.adobe.com/thread/1516624.

  • How to save iMovie project as .mov file?

    That's my question!

    Dr.E wrote:
    How to save iMovie project as .mov file?
    That's my question!
    Share/Export with Quicktime
    That's my answer, and Welcome, Dr.E to the  boards ...

  • How to save video messages to a file on Mac?

    How to save video messages to a file on Mac? Please, help. Thank you.Claire

    Claire, I use a Mac w/ the latest copy of Skype and it IS possible to first play the file and then download it.  Steps are available for it but it involves some technical wizardry that can be above some muggles.   Then I found a post where some some VERY bright person made a small downloaded program for the Mac which once the video message has been played recently, you can run this program and download the file for safe keeping ( ALWAYS BACKUP or store it in the cloud for safety )  Kudos are deserved to user alvarop for this! http://community.skype.com/t5/Mac/Video-Message-Exporter-App/m-p/2351609 It worked for me to download a video message that I recently played back.  Let me know if you need any assistance getting it downloaded you can PM me on here and I'll be happy to help with a screenshare. Best Regards,-Shawn G

  • How to save query result in excel file

    Hi all,
    How to save query result in excel file from sql*plus tool.
    thank you

    Do you really need an Excel file (binary) or a simple CSV?
    If you just need a CSV then search for DUMP_CSV at http://asktom.oracle.com or at this forum
    If you need formatting and/or multiple worksheets then you can use free tools like
    https://xml-spreadsheet.samplecode.oracle.com/ or https://exceldocumenttype.samplecode.oracle.com/
    Regards
    Marcus

  • How to save infopath form in XML file

    How to save InfoPath form in XML file.
    I want every new record in InfoPath form save in XML file and also retrieve these records from XML like databases

    Hi,
    Based on your description, my understanding is that you want to save InfoPath form in XML file.
    I have done a test in my SharePoint, Do the following steps:
    Create an form library and publish an InfoPath form into it, Then when we create new document into the form library, it is by default that InfoPath form is saved in XML file.
    Best Regards,
    Lisa Chen
    Lisa Chen
    TechNet Community Support

  • How to save the content of a JTextArea into a txt file?

    Hi, I want to save the content of a JTextArea into a txt file line by line. Here is part of my code(catch IOException part is omitted):
    String s = textArea.getText();
    File file = new File("file.txt");
    BufferedWriter bw = new BufferedWriter(new FileWriter(file));
    bw.write(s);
    bw.close();
    But I found in the file all is binary code instead of text, any people can help me?
    Thanks in advance

    I can see text in the file now, but the problem is
    when I write three lines in textarea, for example
    111
    222
    333
    then I open the txt file with notepad, it is
    111222333
    How to save them line by line? Use a PrintWriter. It lets you write lines (it's the same class as System.out).
    http://java.sun.com/j2se/1.4/docs/api/java/io/PrintWriter.html

  • Unable to save JTextPane content

    Hello,
    I'm having a problem to save the content of a JTextPane with HTMLEDitorKit. ( With RTF it works fine).
    Everytime I'm trying to save what I have typed into the panel, the file h&as only the folllowing tags.
    <html>
      <head>
      </head>
      <body>
        <p style="margin-top: 0">
        </p>
      </body>
    </html>I do not undrstand how all this works and how to do. Thanks in advance for any idea.
    Gege
    Here is just a sample,code
    package test;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStream;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.text.JTextComponent;
    import javax.swing.text.html.HTMLDocument;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.parser.ParserDelegator;
    public class SaveHTML
      public static void main(String args[]) {
      JFrame frame = new JFrame("Saving Sample");
       Container content = frame.getContentPane();
        final JEditorPane editorPane = new JEditorPane();
        editorPane.setEditable(true);
        JScrollPane scrollPane = new JScrollPane(editorPane);
        content.add(scrollPane, BorderLayout.CENTER);
        editorPane.setEditorKit(new HTMLEditorKit());
        JPanel panel = new JPanel();
        // Setup actions 
        Action saveAction = new AbstractAction() {
              private static final long serialVersionUID = 1L;
              { putValue(Action.NAME, "Save");}
          public void actionPerformed(ActionEvent e) {
            doSaveCommand(editorPane, filename);
        JButton saveButton = new JButton(saveAction);
         panel.add(saveButton);
         content.add(panel, BorderLayout.SOUTH);
          frame.setSize(250, 150);
         frame.setVisible(true);
    public static void doSaveCommand(JTextComponent textComponent,  String filename)
       try
               OutputStream writer = new ByteArrayOutputStream();
                 HTMLEditorKit htmlKit = new HTMLEditorKit();
               HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
               int le = htmlDoc.getLength();
                 OutputStream out2 = new FileOutputStream(new File("filename.htm"));
                 htmlKit.write(out2, htmlDoc, 0, le);
                 out2.close();
                 System.out.println("Saved");
      catch (Exception ex) { ex.printStackTrace(); } 
    }

    Thanks both of you for your reply. As suggested by fgb, I use the component.getDocument() , and it works fine.
    I haven't tested yet component.write. I'll try later to see the difference.
    Now I need to see whether I can re reload it into an empty JTextPane. I have found an example on the web, I'm going to try it.
    Many thanks again.

  • How to get the content of text file to write in JTextArea?

    Hello,
    I have text area and File chooser..
    i wanna the content of choosed file to be written into text area..
    I have this code:
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.*;
    public class Test_Stemmer extends JFrame {
    public Test_Stemmer() {
    super("Arabic Stemmer..");
    setSize(350, 470);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    JButton openButton = new JButton("Open");
    JButton saveButton = new JButton("Save");
    JButton dirButton = new JButton("Pick Dir");
    JTextArea ta=new JTextArea("File will be written here", 10, 25);
    JTextArea ta2=new JTextArea("Stemmed File will be written here", 10, 25);
    final JLabel statusbar =
                  new JLabel("Output of your selection will go here");
    // Create a file chooser that opens up as an Open dialog
    openButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent ae) {
         JFileChooser chooser = new JFileChooser();
         chooser.setMultiSelectionEnabled(true);
         int option = chooser.showOpenDialog(Test_Stemmer.this);
         if (option == JFileChooser.APPROVE_OPTION) {
           File[] sf = chooser.getSelectedFiles();
           String filelist = "nothing";
           if (sf.length > 0) filelist = sf[0].getName();
           for (int i = 1; i < sf.length; i++) {
             filelist += ", " + sf.getName();
    statusbar.setText("You chose " + filelist);
    else {
    statusbar.setText("You canceled.");
    // Create a file chooser that opens up as a Save dialog
    saveButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    JFileChooser chooser = new JFileChooser();
    int option = chooser.showSaveDialog(Test_Stemmer.this);
    if (option == JFileChooser.APPROVE_OPTION) {
    statusbar.setText("You saved " + ((chooser.getSelectedFile()!=null)?
    chooser.getSelectedFile().getName():"nothing"));
    else {
    statusbar.setText("You canceled.");
    // Create a file chooser that allows you to pick a directory
    // rather than a file
    dirButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int option = chooser.showOpenDialog(Test_Stemmer.this);
    if (option == JFileChooser.APPROVE_OPTION) {
    statusbar.setText("You opened " + ((chooser.getSelectedFile()!=null)?
    chooser.getSelectedFile().getName():"nothing"));
    else {
    statusbar.setText("You canceled.");
    c.add(openButton);
    c.add(saveButton);
    c.add(dirButton);
    c.add(statusbar);
    c.add(ta);
    c.add(ta2);
    public static void main(String args[]) {
    Test_Stemmer sfc = new Test_Stemmer();
    sfc.setVisible(true);
    }could you please help me, and tell me what to add or to modify,,
    Thank you..                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    realahmed8 wrote:
    thanks masijade,
    i have filter the file chooser for only text files,
    but i still don't know how to use FileReader to put text file content to the text area (ta) ..
    please tell me how and where to use it..How? -- See the IO Tutorials on Sun for the FileReader (and I assume you know how to call setText and append in the JTextArea).
    Where? -- In the actionPerformed method (better would be a separate thread that is triggered through the actionPerformed method, but that is probably beyond you at the moment), of course.
    Give it a try.

  • Saving JTextPane contents to a File (Images Also)

    How do I save the contents of a JTextPane which contains Images ?
    When I try to save, it just saves the text part skips images.
    Could someone write a little sample code here ?
    Thank you.
    Edited by: Butters on Mar 15, 2008 5:29 PM

    You get the image, and then write it out. If you are saving loading the images yourself, then save the image before you loose all references to it--while you still have the image in memory, not from the JTextPane.

Maybe you are looking for

  • Apple TV with Multiple Mac's in the same house & a PC

    Question-- I recently got a used Apple TV for christmas. I successfully synched it with my Macbook but I haven't tried to synch the content from my iMac or PC. I'm wondering if I can synch content from two seperate mac's & my PC and not have the cont

  • JRockit with -Xmanagement & JBoss 4 does not start

    Hi, I tried to start JBoss 4 with the management console: a fresh JBoss 4, just compiled from source with no special application on it, that starts alright with JRockit without the management console (and also with Sun JVM jdk 1.5). Does anybody know

  • Mail 2.1 CTL-Click

    Since upgrading recently to OSX 10.4.7 and Mail 2.1 any time I use CTL-Click (or Right-Click as programmed on my Mouse) the Mail program hangs in a beach ball state and Force Quit shows the application as Not Responding. The only way out is to Force

  • OUI-10055:Error in accessing the registry - webcenter content installation

    Hi All, I get this OUI error while installing Webcenter Content 11g. I checked the registry permission on HKEY_LOCAL_MACHINE -> Software -> Oracle and it has necessary permission for the user. Can someone help me identify what other registry location

  • Restoring the window size and position of Disk Utility

    I took my mac-mini to a memory upgrade this afternoon, and the guy, who was testing the new configuration of my mini, started, and changed the size of the disk utility window. I hate so much if someone tweaks my personal stuff/setting in their image