problems in recordstore
Hi all,
I am completely new to java/j2me. I am trying to write and read to and from a recordstore. The problems that i am facing are
1. If I add both the exit and start command then then none of the command works.
2. If I comment out the exit command line then probably the data gets written into the recordstore but while reading it I print them then it only displays the integer as 0 instead of the value that was assigned to it.
In the code I have marked out the problems I am facing
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class RecordStoreTest
extends MIDlet implements CommandListener
{
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command start;
private RecordStore recordstore = null;
private RecordEnumeration recordEnumeration = null;
public RecordStoreTest()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.SCREEN, 1);
start = new Command("Start", Command.SCREEN, 1);
form = new Form("Mixed Record");
// If I uncomment the line below then none of the Commands work.
//form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp( boolean unconditional )
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
destroyApp(true);
notifyDestroyed();
}
else if (command == start)
{
try
{
recordstore = RecordStore.openRecordStore("myRecordStore", true);
}
catch(Exception error)
{
alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try
{
byte[] outputRecord;
String outputString[] = { "ABC", "XYZ", "PQRS" , "UVW", "OTHERS"};
int outputno[] = {1234, 1567, 1890, 1345, 1789};
int outputval[] = {5000, 6000, 7000, 8000, 9000};
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
for(int x=0; x<5; x++)
{
outputDataStream.writeInt(outputno[x]);
outputDataStream.writeUTF(outputString[x]);
outputDataStream.writeInt(outputval[x]);
outputDataStream.flush();
outputRecord = outputStream.toByteArray();
recordstore.addRecord(outputRecord, 0, outputRecord.length);
}
outputStream.reset();
outputStream.close();
outputDataStream.close();
}
catch(Exception error)
{
alert = new Alert("Error Writing", error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try
{
StringBuffer buffer = new StringBuffer();
byte[] byteInputData = new byte[300];
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
recordEnumeration = recordstore.enumerateRecords(null,null,false);
while(recordEnumeration.hasNextElement())
{
recordstore.getRecord(recordEnumeration.nextRecordId());
// this line prints 0 not the data 1234 written in the recordstore.
System.out.println("Data 1 is : " + inputDataStream.readInt());
buffer.append(inputDataStream.readInt());
buffer.append(" ");
buffer.append(inputDataStream.readUTF());
buffer.append(" ");
buffer.append(inputDataStream.readInt());
buffer.append(" ");
}
}
catch(Exception error)
{
alert = new Alert("Error Reading", error.toString(),null,AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}
}
Thanks a lot
Ayan

