|
File handling application
It's not pretty
Main calls the constructor
Which does all the work
If you can be bothered to convert this to a char data type then you could use a switch here.
On to the main methods called by the menu loop
Given the record number, seek to that position in the file.
Then read the data.
readString is a helper method (later)
|
The application that manipulates the RAF records.
public class MemberDatabase
{
private static String FILENAME = "members.raf";
RandomAccessFile theFile;
public static void main(String[] args) { new MemberDatabase(); }
public MemberDatabase()
{
try
{
theFile = new RandomAccessFile(FILENAME, "rw");
String c = "q";
do
{
output("add, remove, list, quit (a, r, l, q)");
c = input(">");
if (c.equals("a"))
{
addMember();
}
else if (c.equals("r"))
{
deleteMember();
}
else if (c.equals("l"))
{
listMembers();
}
else if (c.equals("q"))
{
theFile.close();
System.exit(0);
}
else
{
output("Command unknown");
}
}while (c != "q");
}
catch(IOException io)
{
output("Some error opening the client data file");
System.exit(1);
}
}
public void addMember()
{
MemberConsole mc = new MemberConsole();
Member m = mc.getMemberConsole();
if (m != null)
{
insert(m);
}
else
{
output("Some error obtaining record");
}
}
public void deleteMember()
{
int n = inputInt("Member id to delete: ");
long f = searchMember(n);
if (f != -1)
{
delete(f);
}
else
{
output("Member not found");
}
}
public void listMembers()
{
for(long x = 0; x <= numRecords(); x++)
{
Member m = readMember(x);
output(m.toString());
}
}
public Member readMember(long recordNumber)
{
try
{
long filePos = recordNumber * Member.RECORD_LENGTH;
if (filePos < theFile.length())
{
theFile.seek(filePos);
int cb = theFile.readInt();
String fb = readString(Member.FIRST_L);
String lb = readString(Member.LAST_L);
boolean pb = theFile.readBoolean();
return new Member(cb, fb, lb, pb);
}
else
{
output("ReadMember: File pos exceeds file length");
return null;
}
}
catch(IOException ioe)
{
System.out.println("ReadMember: " + ioe.getMessage());
return(null);
}
}
public boolean writeMember(Member theRecord, long recordNumber)
{
try
{
theFile.seek(recordNumber * theRecord.RECORD_LENGTH);
theFile.writeInt(theRecord.getCode());
String f = setLength(theRecord.getFirst(), theRecord.FIRST_L);
String l = setLength(theRecord.getLast(), theRecord.LAST_L);
theFile.writeChars(f);
theFile.writeChars(l);
theFile.writeBoolean(theRecord.hasPaid());
return true;
}
catch(IOException ioe)
{
System.out.println("WriteMember: " + ioe.getMessage());
return false;
}
}
public long numRecords()
{
try
{
long fLen = theFile.length();
long rLen = Member.RECORD_LENGTH;
long num = (fLen / rLen) - 1;
return num;
}
catch(IOException ioe)
{
System.out.println("NumRecords: " + ioe.getMessage());
return -1;
}
}
public void append(Member theRecord)
{
writeMember(theRecord, numRecords() + 1);
}
public long insert(Member theRecord)
{
long pos = numRecords() + 1;
boolean found = false;
Member theMember;
while ( (pos > 0) && (!found) )
{
theMember = readMember(pos-1);
int mem = theMember.getCode();
int rec = theRecord.getCode();
if (mem > rec)
{
found = true;
}
else
{
writeMember(theMember, pos);
pos = pos - 1;
}
}
writeMember(theRecord, pos);
return pos;
}
public long searchMember(long id)
{
long x = 0;
boolean found = false;
while( (!found) && (x <= numRecords()) )
{
Member theRecord = readMember(x);
found = (id == theRecord.getCode());
x++;
}
if (found)
{
return (x - 1);
}
else
{
return -1;
}
}
public void delete(long pos)
{
long last = numRecords();
Member theRecord;
for(long x = (pos + 1); x <= last; x++)
{
theRecord = readMember(x);
writeMember(theRecord, x - 1);
}
try
{
long newLength = theFile.length() - Member.RECORD_LENGTH;
theFile.setLength(newLength);
}
catch(IOException ioe)
{
System.out.println("Error truncating file: " + ioe.getMessage());
}
}
private String setLength(String s, int x)
{
StringBuffer sb = new StringBuffer(s);
sb.setLength(x);
return sb.toString();
}
private String readString(int len)
{
try
{
byte[] b = new byte[len];
for(int x = 0; x < len; x++)
{
b[x] = (byte) theFile.readChar();
}
return new String(b);
}
catch(IOException ioe)
{
System.out.println("Error reading chars: " + ioe.getMessage());
return "EEEE";
}
}
You need to add the IBIO methods here again.
Related: [ Java home | Files home | Previous: Console ]
|
An actual RAF
|