PDA

View Full Version : Problems Printing to a text file


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:

brad211987
11-18-2009, 01:45 PM
Here is a quick sample from a google search of appending to a text file:


try {
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
out.close();
} catch (IOException e) {
}
Take a look at the FileWriter class here: http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html

In particular this constructor:

FileWriter (http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29)(File (http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html) file, boolean append) Setting the second parameter to true will append to the file you open rather than overwriting it.

1zebedee23
11-18-2009, 02:23 PM
Here is a quick sample from a google search of appending to a text file:


try {
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
out.close();
} catch (IOException e) {
}
Take a look at the FileWriter class here: http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html

In particular this constructor:

FileWriter (http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29)(File (http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html) file, boolean append) Setting the second parameter to true will append to the file you open rather than overwriting it.

Thanks for the help there. I'm under the impression that you are unable to write an arraylist into a text file?

BufferedWriter out = new BufferedWriter(new FileWriter("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/gridtest/address.txt",true));

out.write(addressDataOne);

When I try this it doesnt like me trying to write the arraylist "addressDataOne" into the text file. Does this mean I need to put all that data into a string array?

brad211987
11-18-2009, 02:29 PM
No, you have an array list of strings, you just need to iterate over it and write out each index, for example:


ArrayList<String> list;

list.add("one");
list.add("two");
list.add("three");

BufferedWriter out = new BufferedWriter(new FileWriter("file.txt", true));

for (String value : list)
{
out.write(value);
}


Something like that.....this uses the java 5(or 6?) for each loop to iterate over an array list of strings, and print each one to the output stream.

1zebedee23
11-18-2009, 02:54 PM
No, you have an array list of strings, you just need to iterate over it and write out each index, for example:


ArrayList<String> list;

list.add("one");
list.add("two");
list.add("three");

BufferedWriter out = new BufferedWriter(new FileWriter("file.txt", true));

for (String value : list)
{
out.write(value);
}


Something like that.....this uses the java 5(or 6?) for each loop to iterate over an array list of strings, and print each one to the output stream.

Fantastic that has helped a lot, I've come up with this now:

BufferedWriter out = new BufferedWriter(new FileWriter("C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/gridtest/address.txt",true));

for(String bean : addressDataOne){
out.write(bean);
}

out.newLine();
out.close();

I'm assuming I will now be able to make my program search through the data in the text file to find records? (not asking you how to do it just want to make sure i'm going in the right direction).

Thanks for all your help so far, you are really helping to calm me down here!

Zeb

brad211987
11-18-2009, 02:55 PM
Absolutely, you can open it with a scanner or file reader and search however you want, line by line etc...