Saving a string to a file

Discussion in 'Java' started by jay, Jan 30, 2006.

  1. jay

    jay Guest

    Hi all,
    I am trying to save a string that I get from a JTextArea like this:

    ---------
    String s = textArea.getText();
    ---------

    After getting the text I save it using the following code

    ---------------Simplified to reduce number of lines----------
    fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = fc.showSaveDialog(frame);
    try
    {
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    SF = (file.toString() + ".txt");
    FileOutputStream fStream = new
    FileOutputStream(SF);
    ObjectOutputStream stream = new
    ObjectOutputStream(fStream);
    stream.writeObject(s);
    //stream.writeBytes(s);
    stream.flush();
    stream.close();
    fStream.close();
    }
    }catch (Exception e) {
    JOptionPane op = new JOptionPane();
    op.showMessageDialog(null,"A document writing error has
    occured");
    }
    -----------------------------------------------------------------------
    This will save everything correctly but it adds some additional
    characters in front of the file.
    That is if the string 's' contains: this is a test file
    After saving it the file itself has some unreadable characters in front
    of the actual text.

    Does anyone have any ideas of why these characters are being added to
    the string once I save it to a file I have tried both
    stream.writeObject(s) and stream.writeBytes(s) but they both add the
    extra characters.

    I know that the string 's' doesn't contain the extra characters because
    I print it just before saving and it prints as expected.
    jay, Jan 30, 2006
    #1
    1. Advertising

  2. jay

    Roedy Green Guest

    On 29 Jan 2006 19:07:25 -0800, "jay" <> wrote,
    quoted or indirectly quoted someone who said :

    >FileOutputStream fStream = new
    >FileOutputStream(SF);
    > ObjectOutputStream stream = new
    >ObjectOutputStream(fStream);
    > stream.writeObject(s);
    > //stream.writeBytes(s);
    > stream.flush();
    > stream.close();
    > fStream.close();


    this is using a flame thrower to kill a fly. You could do that more
    easily with a DataOutputStream if you don't intend anyone to read it
    or a FileWriter if you do. See
    http://mindprod.com/applets/fileio.html
    for sample code.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 30, 2006
    #2
    1. Advertising

  3. jay

    Venky Guest

    May be you can do this way:

    File file = fc.getSelectedFile();
    FileOutputStream fStream = new FileOutputStream(file);
    fstream.write(s.getBytes());
    fstream.close();
    Venky, Jan 30, 2006
    #3
  4. jay

    jay Guest

    Thanks for your replies, they were definitively a big help.
    jay, Jan 30, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kandell
    Replies:
    4
    Views:
    4,152
    eeebop
    Dec 10, 2004
  2. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,505
    Luis Esteban Valencia
    Jan 6, 2005
  3. jameson737

    java saving String to file

    jameson737, Sep 29, 2006, in forum: Java
    Replies:
    0
    Views:
    13,395
    jameson737
    Sep 29, 2006
  4. Rune Allnor
    Replies:
    3
    Views:
    366
    Richard Herring
    Jan 15, 2010
  5. Lovely Angel For You

    Saving Images While Saving ASP Pages !

    Lovely Angel For You, Oct 2, 2003, in forum: ASP General
    Replies:
    1
    Views:
    204
    Curt_C [MVP]
    Oct 3, 2003
Loading...

Share This Page