Results 1 to 3 of 3
  1. #1
    jadaleus is offline Member
    Join Date
    Oct 2008
    Posts
    12
    Rep Power
    0

    Default Saving to a Text File

    What is the Code if yur trying to save and want to save to the same text [B]WITHOUT replacing the existing text. Instead the next goes to next line.

    here the code Im using:

    if(event.getSource() == signupButton) {

    try {

    outFile1 = new PrintWriter ( new BufferedWriter( new FileWriter("register.txt")));
    outFile1.print(usernameField.getText()+", ");
    outFile1.print(password.getText()+", ");
    outFile1.close();
    JOptionPane.showMessageDialog(null,"Data have been Saved");

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,274
    Rep Power
    25

    Default

    If you want to "APPEND" your text to the end of an existing file, read the API doc for the file class and look for an arg that says to append to that file.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Read more about FileWriter class on the API.

    Java Code:
    outFile1 = new PrintWriter ( new BufferedWriter( new FileWriter("register.txt", true)));
    I wonder why are you using such an annoying way to write data to a file. Seems to me you no need to use PrintWriter instance there.

Similar Threads

  1. [SOLVED] jfilechooser for saving a file....?
    By prabhurangan in forum AWT / Swing
    Replies: 10
    Last Post: 10-06-2013, 10:17 PM
  2. Saving and updating a single file
    By AkisV in forum Advanced Java
    Replies: 1
    Last Post: 09-27-2008, 02:42 PM
  3. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 04:04 PM
  4. Saving data in an XML file
    By Thez in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •