Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 835
  • Last Modified:

RandomAccessFile: Add a record will overwrite the existing records.

I need help from experts! I added a record to a text file by using RandomAccessFile. The record  added (and overwrite) the first record.  Any suggestion?

RandomAccessFile rf = new RandomAccessFile("List.txt", "rw");
rf.writeBytes(System.getProperty("line.separator"));
String newRecord = "add me in the last record";
rf.writeBytes(newRecord);
rf.close();

0
Wonder19
Asked:
Wonder19
1 Solution
 
CEHJCommented:
You need to seek to the end of the file first

RandomAccessFile rf = new RandomAccessFile("List.txt", "rw");
rf.seek(rf.length());
0
 
Wonder19Author Commented:
it works well.  Thanks for your quick response! THANK YOU!
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now