Link to home
Start Free TrialLog in
Avatar of Wonder19
Wonder19

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wonder19
Wonder19

ASKER

sorry that I'm really a beginner.  Can you show me some directions? appreciate!
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)
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();
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial