Sunday, October 20, 2013

RecEnum

import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class RecEnum
extends MIDlet implements CommandListener
{
private Display d;
private Alert a;
private Form f;
private Command exit;
private Command start;
private RecordStore rec = null;
private RecordEnumeration r = null;
public RecEnum()
{
d = Display.getDisplay(this);
exit = new Command("Exit", Command.SCREEN, 1);
start = new Command("Start", Command.SCREEN, 1);
f = new Form("RecordEnumeration");
f.addCommand(exit);
f.addCommand(start);
f.setCommandListener(this);
}
public void startApp()
{
d.setCurrent(f);
}
public void pauseApp()
{
}
public void destroyApp( boolean un )
        {
}
public void commandAction(Command c,
Displayable dd)
{
    if (c == exit)
{
destroyApp(true);
notifyDestroyed();
}
else if (c == start)
{
try
{
rec = RecordStore.openRecordStore(
"myRecordStore", true );
}
catch (Exception error)
{
a = new Alert("Error Creating",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
try
{
String outputData[] = {"First Record",
"Second Record", "Third Record"};
for (int x = 0; x < 3; x++)
{
byte[] byteOutputData = outputData[x].getBytes();
rec.addRecord(byteOutputData,
0, byteOutputData.length);
}
}
catch ( Exception error)
{
a = new Alert("Error Writing",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
try
{
StringBuffer buffer = new StringBuffer();
r =
rec.enumerateRecords(null, null, false);
while (r.hasNextElement())
{
buffer.append(new String(r.nextRecord()));
buffer.append("\n");
}
a = new Alert("Reading",
buffer.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
catch (Exception error)
{
a = new Alert("Error Reading",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
try
{
rec.closeRecordStore();
}
catch (Exception error)
{
a = new Alert("Error Closing",
error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
if (RecordStore.listRecordStores() != null)
{
try
{
    RecordStore.deleteRecordStore("myRecordStore");
r.destroy();
}
catch (Exception error)
{
a = new Alert("Error Removing",error.toString(), null, AlertType.WARNING);
a.setTimeout(Alert.FOREVER);
d.setCurrent(a);
}
}
}
}
}

No comments:

Post a Comment