Results 1 to 4 of 4
  1. #1
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Writing data to file, but not overwriting it.

    This is my DataBase class, which has a GUI. On GUI there is a "Add" button and text field. After i have filled text field with name and clicked "Add", it overwrites Users.txt file with text field input. My question is, how can I make so that after I have clicked "Add", it doesn't overwrite existing file, but it adds another name on second line, if there happens to be a name on first line already.

    Java Code:
    class DataBase {
       
       private BufferedWriter fileStream;
       
     //  private Formatter x;
       
       public void openFile() {
          try {
             fileStream = new BufferedWriter( new FileWriter("Users.txt"));
          } catch (IOException e) {
               e.printStackTrace();
          }
               // x = new Formatter("users.txt");
       }
       
       public void addUser(String firstName,String lastName) {
      //    x.format("%s\t%s\n",firstName,lastName );
          try {
             fileStream.write(String.format("%s\t%s\n", firstName,lastName));
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
       
       public void closeFile() {
        //  x.close();
          try {
             fileStream.close();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Writing data to file, but not overwriting it.

    Read the API documentation for the FileWriter class; it has another constructor that makes the writer write passed the end of the original file, i.e. it appends new content.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing data to file, but not overwriting it.

    FileWriter has a second constructor, one that takes a boolean that if called will solve your problem. Please have a look at the FileWriter API for more on this: FileWriter API

    edit: gak! scooped by a Grolsch-swilling Dutchman!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Writing data to file, but not overwriting it.

    Quote Originally Posted by Fubarable View Post
    edit: gak! scooped by a Grolsch-swilling Dutchman!
    <diabolical laughter>giggle, giggle</diabolical laughter>, man I'm feeling so speedy tonight, I almost answer questions before they were asked! I go voooooom!

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need Help - Writing Data to File
    By uday.bhagwat7 in forum New To Java
    Replies: 0
    Last Post: 02-17-2011, 04:13 AM
  2. Writing data into a file Please Help!
    By Richman118 in forum New To Java
    Replies: 3
    Last Post: 12-17-2010, 02:28 AM
  3. Array overwriting data
    By hobo in forum New To Java
    Replies: 7
    Last Post: 10-27-2010, 01:29 PM
  4. problem with writing new data into file
    By sara12345 in forum New To Java
    Replies: 2
    Last Post: 04-13-2010, 02:34 AM

Posting Permissions

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