Categories: Java | Java ME | Code Examples | Files/Data
This page was last modified 01:28, 4 July 2008.
How to store Data in RMS
From Forum Nokia Wiki
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[] = {"senthil","kumar","k","sri","vas","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); } } } } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I store data/cookies on the phone? | milindgr | Browsing and Mark-ups | 0 | 2002-05-16 11:10 |
| way to read a 5mb file in a series 40 phone | dil_strz | Mobile Java General | 4 | 2006-04-27 08:40 |
| Transferring RMS data over infrared | dan84 | Mobile Java General | 1 | 2005-07-02 14:55 |
| Save Image | dineshk | Mobile Java General | 29 | 2008-05-15 10:51 |
| Write to txt file in res folder | euglena9 | Mobile Java General | 4 | 2006-05-26 06:48 |
