×
Namespaces

Variants
Actions
Revision as of 05:07, 7 September 2009 by valderind4 (Talk | contribs)

Como armazenar dados usando RMS, em Java ME

From Nokia Developer Wiki
Jump to: navigation, search


Original em: How to store Data in RMS

import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class ReadWriteRMS
extends MIDlet implements CommandListener {
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command start;
private Command delete;
 
 
private RecordStore recordstore,recordstore1,recordstore2,recordstore3,recordstore4,recordstore5 = null;
RecordEnumeration recEnum;
 
public ReadWriteRMS() {
display = Display.getDisplay(this);
exit = new Command("Exit", Command.EXIT, 1);
start = new Command("Start", Command.SCREEN, 1);
delete = new Command("Delete",Command.SCREEN,2);
form = new Form("Mixed Record");
form.addCommand(exit);
form.addCommand(start);
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 == start) {
try {
 
recordstore = RecordStore.openRecordStore("myRecordStore", true );
recordstore1 = RecordStore.openRecordStore("1myRecordStore", true );
recordstore2 = RecordStore.openRecordStore("2myRecordStore", true );
recordstore3 = RecordStore.openRecordStore("3myRecordStore", true );
 
 
} catch (Exception error) {
error.printStackTrace();
alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try {
byte[] outputRecord;
 
 
String outputString[] = {"Rajesh","Chandra","k","Raj","Chand","k"};
int outputInteger[] = {25,21,20,23,34,34};
boolean outputBoolean[] = {true,false,true,true,false,true};
 
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream outputDataStream = new DataOutputStream(outputStream);
 
for(int i=0; i<outputString.length;i++) {
outputDataStream.writeUTF(outputString[i]);
outputDataStream.writeBoolean(outputBoolean[i]);
outputDataStream.writeInt(outputInteger[i]);
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);
}
 
// Read records from RMS.........
try {
/* String inputString = null;
int inputInteger = 0;
boolean inputBoolean = false; */

 
StringBuffer buffer = new StringBuffer();
 
byte[] byteInputData = new byte[100];
ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
DataInputStream inputDataStream = new DataInputStream(inputStream);
int numrec = recordstore.getNumRecords();
System.out.println(" Num rec "+numrec);
for(int i=1;i<=numrec;i++) {
// recordstore.getRecord(i);
recordstore.getRecord(i,byteInputData,0);
buffer.append(inputDataStream.readUTF());
buffer.append("\n");
buffer.append(inputDataStream.readBoolean());
buffer.append("\n");
buffer.append(inputDataStream.readInt());
buffer.append("\n");
alert = new Alert("Reading", buffer.toString(),
null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
/* recEnum = recordstore.enumerateRecords(null,null,false);
while (recEnum.hasNextElement())
{
recordstore.getRecord(recEnum.nextRecordId(),byteInputData,0);
buffer.append(inputDataStream.readUTF());
buffer.append("\n");
buffer.append(inputDataStream.readBoolean());
buffer.append("\n");
buffer.append(inputDataStream.readInt());
buffer.append("\n");
alert = new Alert("Reading", buffer.toString(),
null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
*/

inputStream.close();
inputDataStream.close();
} catch (Exception error) {
alert = new Alert("Error Reading",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try {
recordstore.closeRecordStore();
} catch (Exception error) {
alert = new Alert("Error Closing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
 
else if( command == delete) {
 
if (RecordStore.listRecordStores() != null) {
try {
RecordStore.deleteRecordStore("myRecordStore");
} catch (Exception error) {
alert = new Alert("Error Removing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}
}
}
96 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×