Wednesday, August 6, 2008

J2ME RMS

package rms;


import javax.microedition.midlet.*;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.lcdui.*;
import javax.swing.JLabel;

import java.awt.Font;
import java.io.*;

public class ReadWriteRecord
extends MIDlet implements CommandListener {

private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command read,readByte;
private Command write,writeByte;
private Command delete;


private RecordStore recordstoreRead,recordstoreWrite,recordstoreDelete = null;
RecordEnumeration recEnum;

public ReadWriteRecord() {
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
read = new Command("Read", Command.SCREEN, 1);
readByte = new Command("Read Byte", Command.SCREEN, 1);
write = new Command("Write", Command.SCREEN, 1);
writeByte = new Command("Write Byte", Command.SCREEN, 1);
delete = new Command("Delete",Command.SCREEN,2);
//////////////////////////
//Font font = new Font("TAU_Elango_Ragham",22,22);
///////////////////////////
form = new Form("Mixed Record " + "\"வாழ்க வளமுடன்\"");
form.addCommand(exit);
form.addCommand(read);
form.addCommand(write);
form.addCommand(readByte);
form.addCommand(writeByte);
form.addCommand(delete);
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 == read)
{
doRead();
}
else if (command == write)
{
doWrite();
}
else if (command == readByte)
{
doReadByte();
}
else if (command == writeByte)
{
doWriteByte();
}
else if (command == delete)
{
doDelete();
}

}

public void doRead()
{
try
{

recordstoreRead = RecordStore.openRecordStore("myRecordStoreUTF", true );

System.out.println("NumRecords : " + recordstoreRead.getNumRecords());

for (int i = 1; i <= recordstoreRead.getNumRecords(); i++)
{
byte[] byteInputData = new byte[50];
recordstoreRead.getRecord(i, byteInputData, 0);
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);

StringItem idItem = new StringItem("Id: ", String.valueOf(inputDataStream.readInt()));
StringItem nameItem = new StringItem("Name :", inputDataStream.readUTF());
StringItem ageItem = new StringItem("Age :", inputDataStream.readUTF());
StringItem sexItem = new StringItem("Sex :", inputDataStream.readUTF());

form.append(idItem);
form.append(nameItem);
form.append(ageItem);
form.append(sexItem);
}

Alert a = new Alert("Success", "Read Records finished", null, AlertType.INFO);
Display.getDisplay(this).setCurrent(a);

}
catch(Exception ex)
{
Alert a = new Alert("Fail to read", ex.getMessage(), null, AlertType.ERROR);
Display.getDisplay(this).setCurrent(a);

System.out.println("Unable to read the Record");
ex.printStackTrace();
}
}

public void doWrite()
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream( bout );

try
{

recordstoreWrite = RecordStore.openRecordStore("myRecordStoreUTF", true );

int input_id = 1;
String input_name = "Senthil";
String input_age = "29";
String input_sex = "Male";

byte dataArray[];

try
{
dout.writeInt( input_id );
dout.writeUTF( input_name );
dout.writeUTF( input_age );
dout.writeUTF( input_sex);
dout.close();

dataArray = bout.toByteArray();
recordstoreWrite.addRecord( dataArray, 0, dataArray.length );
}
catch( RecordStoreException e )
{
// handle RMS error here
}
catch( IOException e )
{
// handle IO error here
}

recordstoreWrite.closeRecordStore();

Alert a = new Alert("Success to Write", "Record written into File", null, AlertType.INFO);
Display.getDisplay(this).setCurrent(a);

}
catch(Exception ex)
{
Alert a = new Alert("Fail to write", ex.getMessage(), null, AlertType.ERROR);
Display.getDisplay(this).setCurrent(a);

System.out.println("Unable to write the Record");
ex.printStackTrace();
}

}
}

No comments: