aspose file tools
The moose likes I/O and Streams and the fly likes using the seek() method to prevent data overwriting in a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "using the seek() method to prevent data overwriting in a text file" Watch "using the seek() method to prevent data overwriting in a text file" New topic
Author

using the seek() method to prevent data overwriting in a text file

Igbe Jr. Chukwudi
Greenhorn

Joined: Nov 29, 2010
Posts: 12
please how can i use the RandomAccessFile method-the seek(long n) method to prevent overwritting of data previous written to a text file when i want to perform a new write operation to the same file.....thanks


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();

}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 19338
    
  10

If you don't want to overwrite data which is in the file already, then you would need to go to the end of the file.

If it were me I would just use an ordinary FileWriter, created with the constructor which preserves the data and makes all the writes append after that. I wouldn't mess about with RandomAccessFile. I also would avoid RandomAccessFile because it's byte-oriented and not char-oriented.
 
rocket mass heater kickstarter
 
subject: using the seek() method to prevent data overwriting in a text file