home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 1.4 KB | 64 lines |
- import java.io.*;
-
- public class DB {
- private static final String FILE_NAME = "test.db";
- private RandomAccessFile file;
- private File test;
- private boolean open;
-
- public DB() throws IOException {
- try {
- test = new File(FILE_NAME);
- file = new RandomAccessFile(FILE_NAME, "rw");
- open = true;
- } catch (IOException x) {
- close();
- throw x;
- }
- }
-
- public void close() {
- if (open) {
- try {
- file.close();
- } catch (IOException x) {
- System.out.println(x.getMessage());
- } finally {
- open = false;
- }
- }
- }
-
- public void finalize() throws Throwable {
- close();
- super.finalize();
- }
-
- public void rewind() throws IOException {
- file.seek(0);
- }
-
- public boolean moreRecords() throws IOException {
- return (file.getFilePointer() < file.length());
- }
-
- public EmployeeRecord readRecord()
- throws IOException, EOFException
- {
- String ssn = file.readUTF();
- String name = file.readUTF();
- EmployeeRecord record = new EmployeeRecord(ssn, name);
-
- return record;
- }
-
- public void writeRecord(EmployeeRecord record)
- throws IOException
- {
- file.seek(file.length());
- file.writeUTF(record.getSsn());
- file.writeUTF(record.getName());
- }
-
- }
-