Delete a record using a random access file

This question is related to the Address Book Function thread, which has been closed, considering it too wide.

I was developing a small test application using Random Access File. I am attaching source code to it. I want to delete an entry from a file. I searched for similar streams in stackoverflow Delete a random file access file . These threads suggest setting the flag as remote by record, rather than displaying it. I want to delete it. I tried to search for a record and tried to add a null record to the position of the file pointer. Nothing has happened. Please suggest what is the right way to do this?

I am connecting the source code below.

package ben.io; import java.io.Serializable; /** * @author Ben * This is an object to hold phone book information */ public class PhoneBook implements Serializable { String personName ; String phoneNumber ; String location ; public PhoneBook() { } public PhoneBook(String personName, String phoneNumber, String location) { this.personName = personName; this.phoneNumber = phoneNumber; this.location = location; } public String getPersonName() { return personName; } public void setPersonName(String personName) { this.personName = personName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String toString(){ return "Name: " + personName + " Phone Number: " + phoneNumber + " Address: " + location; } } 

The main class that has all the functionality.

 package ben.io; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import com.sun.xml.internal.ws.util.StringUtils; /** * @author Ben * This class is responsible for testing various operations for Random Access File */ public class RandomAccessFileDriverTest { public static void main(String[] args) throws IOException { //Creating an instance of Random Access File RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\2014\\Habit Course 1 Study Targets\\Studies\\files\\ben.txt","rw"); //Finding size of Random Access File - Size is basically 36 why showing 38? long size = randomAccessFile.length(); System.out.println("Size of Random Access file is: " + size); //Finding position of File Pointer long positionOfFilePointer = randomAccessFile.getFilePointer(); System.out.println("File Pointer Position: " + positionOfFilePointer); //Reading Data from a Random Access File using readLine /* randomAccessFile.seek(3); String data = randomAccessFile.readLine(); System.out.println("Data is: " + data);*/ //Reading bytes from a Random Access File using read() /* byte[] byteData = new byte[100]; randomAccessFile.read(byteData,3,18); String strData = new String(byteData); System.out.println("Data using Byte is: " + strData); */ //Adding record to Random Access File PhoneBook phoneBook = new PhoneBook("Ben","12345","UAE"); //adding record for random access file // randomAccessFile = addRecord(phoneBook,randomAccessFile); //view records for random access file // viewAll(randomAccessFile); //Searching a record - Give a phone number as a key to Search PhoneBook recordSearched = searchRecord("123456",randomAccessFile); if(recordSearched == null){ System.out.println("Input is not valid"); } else{ System.out.println(recordSearched); } //Delete a Record From File deleteRecord("123456",randomAccessFile); //View the records viewAll(randomAccessFile); } public static RandomAccessFile addRecord(PhoneBook phoneBook, RandomAccessFile randomAccessFile) throws IOException { long length = randomAccessFile.length(); //seeking position at end of file randomAccessFile.seek(length); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(phoneBook.getPersonName()).append(":").append(phoneBook.getPhoneNumber()). append(":").append(phoneBook.getLocation()); if(randomAccessFile.getFilePointer() != 0){ randomAccessFile.writeBytes(System.getProperty("line.separator")); } randomAccessFile.writeBytes(stringBuffer.toString()); return randomAccessFile; } public static PhoneBook searchRecord(String phoneNumber , RandomAccessFile randomAccessFile) throws IOException { PhoneBook phoneBook = null ; //Setting file pointer to start of file randomAccessFile.seek(0); String data = randomAccessFile.readLine(); while (data != null ){ String[] recordToBeSearched = data.split(":"); String contactNo = recordToBeSearched[1]; if(contactNo != null && contactNo.equals(phoneNumber)){ phoneBook = new PhoneBook(recordToBeSearched[0],recordToBeSearched[1],recordToBeSearched[2]); break ; } data = randomAccessFile.readLine(); } return phoneBook; } public static void deleteRecord(String phoneNumber, RandomAccessFile randomAccessFile) throws IOException { PhoneBook phoneBook = null ; //Setting file pointer to start of file randomAccessFile.seek(0); String data = randomAccessFile.readLine(); while (data != null ){ String[] recordToBeSearched = data.split(":"); String contactNo = recordToBeSearched[1]; if(contactNo != null && contactNo.equals(phoneNumber)){ String blankData = new String(); randomAccessFile.writeBytes(blankData); break ; } data = randomAccessFile.readLine(); } } public static void viewAll(RandomAccessFile randomAccessFile) throws IOException { String data ; //This is responsible for reading complete file randomAccessFile.seek(0); data = randomAccessFile.readLine(); while (data != null){ System.out.println(data); data = randomAccessFile.readLine(); } } } 

The sample data file used has the following data.

enter image description here

+1
source share
1 answer

There are several problems in this code.

  • You write 12345 and look for 123456 to delete (simple).
  • If you look at what the writeBytes method does, it creates an array of bytes of size equal to the length of the line you are writing. Which is zero in your case, so nothing is written. So you need to write a line whose length matches the number of characters you want to delete (think of xxxxx as a delete line, as indicated in other posts).

Note: you set the pointer to zero at the beginning of the loop, which will change after the first read, so you will need to track the file pointer. Since deletion happens on char to char, you need to find the length of the entry and build the string 'delete'.

Btw, if you want to really get rid of deleted characters, you better read the file into the buffer, do all the reading and writing, and then upload the file back. Just writing space characters (or empty lines) does not move the characters forward.

+2
source

Source: https://habr.com/ru/post/1244877/


All Articles