In: Computer Science
describe the mechanism that supports random file access in Java.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
The heart of the random access file mechanism is the class called java.io.RandomAccessFile. It has all the capabilities of FileInputStream and FileOutputStream. The instance of this class not only helps in creating or opening an existing file but also provide a parameter to specify the mode of operation such as read(r) or write(w) whien opening a file. We can almost visualize a random access file as an array of bytes stored in the file system. An array location, as we know, can be directly accessed if we supply an appropriate index number.
In much the same way, records in the random access file can be directly accessed with the index number supplied through the seek method and subsequently read/write chunks of data from/to the file. Whien traversing through the records, to ascertain if we have reached the end of file, we can either compare the current position of the file pointer (by using the getFilePointer method, which returns the current offset) with the total length of the file itself or, more simply, look for the occurrence of EOFException. EOFException is thrown when the file pointer tries to access beyond the end of file mark of the file. However, if any other error occurs other than EOFException, an IOException is thrown.
Implementing a CRUD Operation
CRUD is basically a database operation. Here we implement it to simulate a database-like implementation of a flat file. Because flat files are not meant to operate like a database, here, the Java code is more of a jugglery to achieve this end. However, this fun exercise can not only enrich you conceptually but also give you an glimpse of actual database storage structure and how difficult/easy is to create one from scratch. The code is simple enough to understand. However, simplification sometime overshadows important details; so is the case here. A lot of detail validation and verification is simply ignored. Nevertheless, one can always modify the following code to achieve a greater end once the basic concept is grasped.
public class FileDatabase { RandomAccessFile file; public FileEditor(String fileString) throws IOException { file = new RandomAccessFile(fileString, "rw"); } public void close() throws IOException { if (file != null) file.close(); } public PersonRecord getRecord(int id) throws IOException { PersonRecord record = new PersonRecord(); if (id < 1) throw new IllegalArgumentException("invalid ID!!"); file.seek((id - 1) * PersonRecord.SIZE); record.readFromFile(file); return record; } public void insertRecord(PersonRecord record) throws IllegalArgumentException, IOException { if (getRecord(record.getId()).getId() != 0) System.out.println("Cannot add new. Record already exists."); else { file.seek((record.getId() - 1) * PersonRecord.SIZE); record.writeToFile(file); } } public void updateRecord(PersonRecord record) throws IllegalArgumentException, IOException { if (getRecord(record.getId()).getId() == 0) System.out.println("Cannot update. Record does not exist."); else { file.seek((record.getId() - 1) * PersonRecord.SIZE); record.writeToFile(file); } } public void deleteRecord(PersonRecord record) throws IllegalArgumentException, IOException { if (getRecord(record.getId()).getId() == 0) System.out.println("Cannot delete. Record does not exist."); else { file.seek((record.getId() - 1) * PersonRecord.SIZE); record = new PersonRecord(); record.writeToFile(file); } } public void showAllRecords() { PersonRecord record = new PersonRecord(); try { file.seek(0); while (true) { do { record.readFromFile(file); } while (record.getId() == 0); System.out.println(record.toString()); } } catch (EOFException ex1) { return; } catch (IOException ex2) { System.err.println("error reading file"); } } }
To test, we can supply some dummy data as follows:
public class Test { public static void main(String[] args) throws IOException { FileEditor fe = new FileEditor ("/home/mano/temp/people.dat"); fe.insertRecord(new PersonRecord(1, "Brian", "Sullivan", "[email protected]")); fe.insertRecord(new PersonRecord(2, "Randal", "Wallace", "[email protected]")); fe.insertRecord(new PersonRecord(3, "Eric", "Bloch", "[email protected]")); fe.insertRecord(new PersonRecord(4, "Kapil", "Ansari", "[email protected]")); fe.showAllRecords(); fe.updateRecord(new PersonRecord(4,"Tony","Li", "[email protected]")); fe.showAllRecords(); fe.deleteRecord(new PersonRecord(1,"Brian","Sullivan", "[email protected]")); fe.showAllRecords(); fe.close(); } }
Kindly revert for any queries
Thanks.