Программирование на Java



Исходный текст приложения DirectFile - часть 2


else if(obj.equals("View records...")) { SimpleDBMS db = new SimpleDBMS( "dbtest.idx", "dbtest.dat"); String szRecords;

szRecords = db.GetRecordByNumber(0) + db.GetRecordByNumber(1) + db.GetRecordByNumber(2);

db.close();

MessageBox mbox; mbox = new MessageBox(szRecords, this, "Database records", true); mbox.show(); }

else if(obj.equals("Content")) { MessageBox mbox; mbox = new MessageBox( "Item Content selected", this, "Dialog from Frame", true); mbox.show(); }

else if(obj.equals("About")) { MessageBox mbox; mbox = new MessageBox( "Item About selected", this, "Dialog from Frame", true); mbox.show(); } else return false; return true; } return false; } }

class MessageBox extends Dialog { Label lbMsg; Button btnOK;

public MessageBox(String sMsg, Frame parent, String sTitle, boolean modal) { super(parent, sTitle, modal); resize(300, 100); setLayout(new GridLayout(2, 1)); lbMsg = new Label(sMsg, Label.CENTER); add(lbMsg); btnOK = new Button("OK"); add(btnOK); }

public boolean handleEvent(Event evt) { if(evt.id == Event.WINDOW_DESTROY) { dispose(); return true; } else return super.handleEvent(evt); }

public boolean action(Event evt, Object obj) { Button btn; if(evt.target instanceof Button) { btn = (Button)evt.target;

if(evt.target.equals(btnOK)) { dispose(); } else return false; return true; } return false; } }

class SimpleDBMS { RandomAccessFile idx; RandomAccessFile dat;

long idxFilePointer = 0;

public SimpleDBMS(String IndexFile, String DataFile) { try { idx = new RandomAccessFile( IndexFile, "rw"); dat = new RandomAccessFile( DataFile, "rw"); } catch(Exception ioe) { System.out.println(ioe.toString()); } }

public void close() { try { idx.close(); dat.close(); } catch(Exception ioe) { System.out.println(ioe.toString()); } }

public void AddRecord(String name, int account) { try { idx.seek(idx.length()); dat.seek(dat.length());

idxFilePointer = dat.getFilePointer();

idx.writeLong(idxFilePointer);

dat.writeBytes(name+ "\r\n"); dat.writeInt(account); } catch(Exception ioe) { System.out.println(ioe.toString()); } }

public String GetRecordByNumber(long nRec) { String sRecord = "<empty>";

try { Integer account; String str = null;

idx.seek(nRec * 8); idxFilePointer = idx.readLong(); dat.seek(idxFilePointer);

str = dat.readLine(); account = new Integer(dat.readInt());

sRecord = new String("> " + account + ", " + str); } catch(Exception ioe) { System.out.println(ioe.toString()); } return sRecord; } }




Содержание  Назад  Вперед