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
That's so far I can go... please help !
RandomAccessFile rf = new RandomAccessFile("List.txt
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
ASKER
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();
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER