• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 881
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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