aspose file tools*
The moose likes I/O and Streams and the fly likes how to write to text files without overwriting the original contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how to write to text files without overwriting the original contents" Watch "how to write to text files without overwriting the original contents" New topic
Author

how to write to text files without overwriting the original contents

Igbe Jr. Chukwudi
Greenhorn

Joined: Nov 29, 2010
Posts: 12
hi
i am writing a program(gui based),on clicking on a button,the values in the gui are retrieved and stored to a text file.my problem is that when ever i refill the gui form with new values and click the button,the former values in the files are overwritten.How can i stop this because i need to read all the values of the students that registered in a year?.......i have tried the seek method but,am not getting it right.here is my code...


private class ButtonHandler implements ActionListener,Serializable {



public void actionPerformed(ActionEvent e) {

Form1 form1 = new Form1();


String x = "";
File file = new File("client.txt");

/**
try {
RandomAccessFile Rfile = new RandomAccessFile("client.txt","rw");
long n = 10;
Rfile.seek(n);
} catch(FileNotFoundException j){}catch(IOException nb){}
*/

try {
FormLogic logic = new FormLogic();


PrintWriter out = new PrintWriter(new FileWriter(file));
logic.setName(nameText.getText());
String schooldata = (String) boxForSchoolsComboBox.getSelectedItem();
String graduatedata = (String)graduateDegreeProgramComboBox.getSelectedItem();
String statedata = (String) stateComboBox.getSelectedItem();
String addressdata = addressText.getText();
String citydata = cityText.getText();
String statdata = statetext.getText();
String zipcodedata = zipcodeText.getText();
String countrycode = countryText.getText();
String countrycodedata = countrycodeText.getText();
String areacodedata = areacodeText.getText();
String phonenodata = phonenumberText.getText();
String emaildata = emailaddressText.getText();
String datebatedata = dateofbirthText.getText();
String citizendata =(String) citizenstatusComboBox.getSelectedItem();
boolean radiodata = maleButton.isSelected();


out.println(schooldata);
out.println(graduatedata);
out.println(logic.getName());



out.println(addressdata);
out.println(citydata);
out.println(statdata);
out.println(zipcodedata);
out.println(countrycode);
out.println(countrycodedata);
out.println(areacodedata);
out.println(phonenodata);
out.println(emaildata);
out.println(datebatedata);
if(radiodata == true) {
out.println("MALE");
}
else {out.println("FEMALE");}


out.close();
/**
RandomAccessFile Rfile = new RandomAccessFile("client.txt","rws");

long n =30;
n++;

Rfile.seek(file.length());
Rfile.seek(n);
Rfile.writeBytes(addressdata);

long m =20;
m++;
Rfile.seek(file.lastModified());
Rfile.seek(file.length());
Rfile.writeBytes(citydata);
Rfile.seek(file.length());


Rfile.close();
*/
}catch (IOException e1){
System.out.print("error opening file");
}

//this.launch();

}


Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Set the FileWriter to append: http://download.oracle.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to write to text files without overwriting the original contents
 
Similar Threads
how to transfer contol to another gui
urgent help needed in JTable !!!!!!!!!!!!!!
using the seek() method to prevent data overwriting in a text file
writing to text files
File transfer using Java Http