• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to write to text files without overwriting the original contents

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}


 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Set the FileWriter to append: http://download.oracle.com/javase/6/docs/api/java/io/FileWriter.html#FileWriter%28java.io.File,%20boolean%29
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic