1zebedee23
11-18-2009, 12:49 PM
Hi there guys, I'm new to the forum and it's a program I'm trying to make at the moment that has driven me to it!
My problem is probably simple if you know what your doing, but as I'm still learning it feels like I've run into a brick wall. I am making an address book and the address book has 9 JTextFields in it, when you press an add button it will save the data in those text fields to an external text file. I've tried loads of different ways to make it do it and I've even managed to make it print into a text file, but then when I try and add some more data it over writes what I have already put in! I'm sure the solution is simple but I've spent ages looking through pages on google and I'm not getting any where!
So here's the code for my add button:
public void actionPerformed(ActionEvent e)
{
try {
ArrayList<String> addressDataOne = new ArrayList<String>();
storeLastName = lastName.getText();
storeFirstName = firstName.getText();
storeAddress = address.getText();
storeAddressTwo = addressTwo.getText();
storeTown = town.getText();
storeCounty = county.getText();
storeHomePhone = homePhone.getText();
storeMobilePhone = mobilePhone.getText();
storePostCode = postCode.getText();
addressDataOne.add(storeFirstName);
addressDataOne.add(storeLastName);
addressDataOne.add(storeAddress);
addressDataOne.add(storeAddressTwo);
addressDataOne.add(storeTown);
addressDataOne.add(storeCounty);
addressDataOne.add(storePostCode);
addressDataOne.add(storeMobilePhone);
addressDataOne.add(storeHomePhone);
System.out.println(addressDataOne);
// PrintStream out = new PrintStream("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/gridtest/address.txt");
// out.println(addressDataOne);
//out.newLine();
//out.close();
BufferedWriter out = new BufferedWriter(new FileWriter("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/gridtest/address.txt",true));
out.write(firstName.getText());
out.newLine();
} catch (IOException ex) {
System.err.println("ERROR:" + ex);
}
}
As you can see I've been playing around a lot trying different methods. I thought I probably need to read the data into an array list and then print that into a txt file but may I'm barking up the wrong tree? The entries need to be saved on separate lines in the text file.
Hope someone can help because I'm ripping my hair out here!
Thanks Zebedee :thumbsup:
My problem is probably simple if you know what your doing, but as I'm still learning it feels like I've run into a brick wall. I am making an address book and the address book has 9 JTextFields in it, when you press an add button it will save the data in those text fields to an external text file. I've tried loads of different ways to make it do it and I've even managed to make it print into a text file, but then when I try and add some more data it over writes what I have already put in! I'm sure the solution is simple but I've spent ages looking through pages on google and I'm not getting any where!
So here's the code for my add button:
public void actionPerformed(ActionEvent e)
{
try {
ArrayList<String> addressDataOne = new ArrayList<String>();
storeLastName = lastName.getText();
storeFirstName = firstName.getText();
storeAddress = address.getText();
storeAddressTwo = addressTwo.getText();
storeTown = town.getText();
storeCounty = county.getText();
storeHomePhone = homePhone.getText();
storeMobilePhone = mobilePhone.getText();
storePostCode = postCode.getText();
addressDataOne.add(storeFirstName);
addressDataOne.add(storeLastName);
addressDataOne.add(storeAddress);
addressDataOne.add(storeAddressTwo);
addressDataOne.add(storeTown);
addressDataOne.add(storeCounty);
addressDataOne.add(storePostCode);
addressDataOne.add(storeMobilePhone);
addressDataOne.add(storeHomePhone);
System.out.println(addressDataOne);
// PrintStream out = new PrintStream("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/gridtest/address.txt");
// out.println(addressDataOne);
//out.newLine();
//out.close();
BufferedWriter out = new BufferedWriter(new FileWriter("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/gridtest/address.txt",true));
out.write(firstName.getText());
out.newLine();
} catch (IOException ex) {
System.err.println("ERROR:" + ex);
}
}
As you can see I've been playing around a lot trying different methods. I thought I probably need to read the data into an array list and then print that into a txt file but may I'm barking up the wrong tree? The entries need to be saved on separate lines in the text file.
Hope someone can help because I'm ripping my hair out here!
Thanks Zebedee :thumbsup: