• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3680
  • Last Modified:

RandomAccessFile delete record

I have tow question on deleting a record and edit a field in a text file by using RandomAccessFile?

That's so far I can go... please help !

RandomAccessFile rf = new RandomAccessFile("List.txt", "rw");
String searchWord = round;
if ((line = rf.readLine()) != null) {
   StringTokenizer st = new StringTokenizer(line,";");
   while (st.hasMoreElements()){
      field1 = result[0];
      field2 = result[1];
      field3 = result[2];

     if (field3 == searchWord) {
        // I can't think of anyway to do here. please help!
     }
  }
}

List.txt is ...
apple;red;round
apple;green;round
orange;orange;round
banana;yellow;long
orange;orange;big
0
Wonder19
Asked:
Wonder19
4 Solutions
 
objectsCommented:
you cannot delete using RAF, you can only overwrite
0
 
Wonder19Author Commented:
sorry that I'm really a beginner.  Can you show me some directions? appreciate!
0
 
objectsCommented:
To delete a line, you'll need to read the file and write it to a new file (without the line you want to delete)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Wonder19Author Commented:
Helloi objects, I put together the codes, but I have an error on fin.read(c); how can I read that pariticular line of record and determine which line to "delete"?

int c;
FileInputStream fin = new FileInputStream(new File("DirectoryList.txt"));
FileOutputStream fout = new FileOutputStream(new File("DirectoryListNew.txt"));
while ((c = fin.read()) != -1) {
   String st = fin.read(c);
   String[] r = st.split(";");
  if (r[2].equals(resourseName)) {
     // do nothing
  } else {
     fout.write(c);
  } // end if
 // end while
fin.close();
fout.close();
0
 
Giant2Commented:
0
 
JoeCommented:
You could use the RandomAccessFile to replace the line with hard spaces.  You could than have a utility that would run when your program is offline to pack the file and remove the dead record space.

Joe
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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