-1

so I'm creating a basic Address book application for fun. I want the user to be able to click the (Add to book) button to save the current information entered into the Address Book.txt file. The problem is the code I'm currently using erases all file contents and only adds in the new entry. What would be a good way to add to the file without deleting contents, or if that's not possible what's a good way to store all the file data and re-enter in to the Address Book.txt file?

Here is my code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;

public class AddressBook extends JFrame{
//Create fields
    public ArrayList<String> data;
    public JTextField name,address,number;
    private JButton addStuff;
//Create the constructor for the Phone book 
    public AddressBook(){
        //set the layout
        setLayout(new FlowLayout());
        //Create instances of fields and give properties
        name = new JTextField("(Name)");
        name.setPreferredSize(new Dimension(200,20));
        address = new JTextField("(Address)");
        address.setPreferredSize(new Dimension(290,20));
        number = new JTextField("(Number)");
        number.setPreferredSize(new Dimension(100,20));
        //create a save button and add functionality 
        addStuff = new JButton("Add to Book");
        addStuff.addActionListener(
                new ActionListener(){



@Override
                    public void actionPerformed(ActionEvent e) {

I Create a way to access and write to the file Address Book.txt, but it seems to be erasing all data when I do

     PrintWriter writer;
                String x = String.format("Name: %s   Number: %s    Address: %s ", name.getText(),number.getText(),address.getText());
                String name = "Address Book.txt";
            try {
                writer = new PrintWriter(name, "UTF-8");
                writer.println(x);
//Go to new line to write info to
                    writer.println("");

                     writer.close();
                    }
                catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                        } 
                catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                        }

                    }
                }
            );
    add(name);
    add(address);
    add(number);
    add(addStuff);
}

I was going to use this to read all the stuff in the text file and save it to a String ArrayList (data) but the information would come out printed weird in the file.

private class ReadFile {

        private Scanner x;

        public void openFile(){
            try{
                x = new Scanner(new File("Address Book.txt"));
            }
            catch(Exception e)
            {
                System.out.println("File not found or something");
            }

        }
        public void readFile(){
            while(x.hasNext()){
                String a = x.next();
                data.add(a);
            }
        }
    }

}

2 Answers2

1

Create your PrintWriter from a FileInputStream set to append instead of truncate:

writer = new PrintWriter(new FileOutputStream(name, true));
Darth Android
  • 3,437
  • 18
  • 19
1

Instead of PrintWriter consider using a FileWriter with append set to true

Writer w = new FileWriter(name, true);
PrintWriter pw = new PrintWriter(w);
....
pw.flush();
pw.close();
w.flush();
w.close();
jdevelop
  • 12,176
  • 10
  • 56
  • 112