Selasa, 03 Desember 2013

mobile 3desember

//ReadStore

public class ReadStore
extends Form
implements CommandListener {
private StoreData midlet;
private Display display;
private Command cmExit;
private StringItem si;

public ReadStore(StoreData midlet, Display display) {
super("Store Data");
this.midlet = midlet;
this.display = display;

int[] code = {1, 2, 3};
String[] book = {"J2EE", "J2ME", "Next"};
boolean[] publish = {true, false, false};

Save save = new Save("db_data");
save.open();
save.saveRecordStream(code, book, publish, 0);
si = new StringItem("", save.readRecordStream());
append(si);
save.close();
save.delete();

cmExit = new Command("Keluar", Command.EXIT, 1);
setCommandListener(this);
addCommand(cmExit);
}

public void commandAction(Command c, Displayable s) {
if (c == cmExit)
midlet.exitMIDlet();
}
}

//StoreData

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class StoreData extends MIDlet{
Display display;
ReadStore read;

public StoreData() {
display = Display.getDisplay(this);
}

public void startApp() {
read = new ReadStore(this, display);
display.setCurrent(read);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void exitMIDlet() {
destroyApp(false);
notifyDestroyed();
}
}

//save

import javax.microedition.rms.*;
import java.io.*;

class Save {
private RecordStore store;
private String storage;
private int amount = 0;

Save(String storage, int amount) {
store = null;
this.amount = amount;
this.storage = storage;
}

Save(String storage){
this.storage = storage;
}

RecordStore getStore() {
return store;
}

boolean open() {
try {
store = RecordStore.openRecordStore(storage, true);
} catch (RecordStoreException rse) {
}

if (store == null)
return false;

return true;
}

void close() {
try {
if (store != null)
store.closeRecordStore();
} catch (RecordStoreException rse) {
}
}

void delete() {
try {
store.deleteRecordStore(storage);
} catch (Exception e) {
}
}

void saveRecord(int recID, String str) {
byte[] rec = str.getBytes();
try {
if (recID == 0)
store.addRecord(rec, 0, rec.length);
else
store.setRecord(recID, rec, 0, rec.length);
} catch (RecordStoreException rse) {
}
}

String readRecord(int recID) {
try {
byte[] recData = new byte[amount];
int len;

len = store.getRecord(recID, recData, 0);
return new String(recData, 0, len);
} catch (RecordStoreException rse) {
return "null";
}
}

void saveRecordStream(int[] iCode, String[] sBook, boolean[] bPublish, int recID) {
try {
ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();
DataOutputStream strmDataType = new DataOutputStream(strmBytes);
byte[] record;

for (int i = 0; i < iCode.length; i++) { strmDataType.writeInt(iCode[i]); strmDataType.writeUTF(sBook[i]); strmDataType.writeBoolean(bPublish[i]); strmDataType.flush(); record = strmBytes.toByteArray(); try { if (recID == 0) store.addRecord(record, 0, record.length); else store.setRecord(recID, record, 0, record.length); } catch (RecordStoreException rse) { } strmBytes.reset(); } strmBytes.close(); strmDataType.close(); } catch (Exception e) { } } String readRecordStream() { try { String rec = ""; byte[] recData = new byte[50]; ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData); DataInputStream strmDataType = new DataInputStream(strmBytes); if (store.getNumRecords() > 0){
int i = 1;
RecordEnumeration re = store.enumerateRecords(null, null, false);
while (re.hasNextElement()){
store.getRecord(re.nextRecordId(), recData, 0);

rec = rec + strmDataType.readInt() + " | " + strmDataType.readUTF() + " | " + strmDataType.readBoolean() + "\n";
strmBytes.reset();
}
re.destroy();
}

strmBytes.close();
strmDataType.close();
return rec;
}catch (Exception e){
return null;
}
}
}

Tidak ada komentar:

Posting Komentar

berkomentarlah untuk membagi ilmumu

Cara Seo Blogger