Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Read Writer button wont add to file.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read Writer button wont add to file.

    my writer reader wont save data and add data to the file. When i run it all it does is overwrites the data. code is :
       String add = nameField.getText();
            String newLine = ("\n");
            File file = new File("C:NewTest.dat");
            try {
    // check whether the file is existed or not
                if (file.exists()) {
     
                    // create a new file if the file is not existed
                    file.createNewFile();
                }
                // new a writer and point the writer to the file
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                // writer the content to the file
                writer.write(newLine);
                writer.write(add);
                // always remember to close the writer
                writer.close();
                writer = null;
            } catch (IOException ab) {
                ab.printStackTrace();
            }
            try {
                // new a reader and point the reader to the file
                BufferedReader reader = new BufferedReader(new FileReader(file));
                nameArea.append(add + newLine);
                reader.close();
                reader = null;
            } catch (FileNotFoundException ab) {
                ab.printStackTrace();
            } catch (IOException ab) {
                ab.printStackTrace();
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read Writer button wont add to file.

    add data to the file
    There is an arg in a constructor that will APPEND data to an existing output file instead of creating a new file. Read the API doc to see how to code it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Read input, read file, find match, and output... URGENT HELP!
    By MooseHead in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 3rd, 2012, 11:01 AM
  2. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  3. [SOLVED] Buffered writer in a .jar file
    By lorider93p in forum Java Theory & Questions
    Replies: 8
    Last Post: February 8th, 2012, 01:03 PM
  4. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM
  5. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM