Showing posts with label Java Code. Show all posts
Showing posts with label Java Code. Show all posts

J2ME RecordStore Example


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

/**
* Preferences Store
*
*Create a class that can persist program preferences. The class will store the preferences into a Record Store.
*Each record will have a variable name and value. Each variable/value pair is stored in a single record.
*The name and value are stored in the database as Strings.
*
*The class should implement these methods:
*public String readVar(RecordStore recStore, String name, String defaultValue)
*public void writeString(RecordStore recStore, String name, String value);
*
* @author calcifer
*
*/

public class PreferenceStore extends MIDlet implements CommandListener{
private Display display;
private Command exit;
private Command write;
private Command read;
private Command back;

private Form inputForm;

private TextBox output = new TextBox("Your Preference:", "", 250, TextField.ANY);
private TextField varName = new TextField("VarName:", "",32,TextField.ANY);
private TextField varValue = new TextField("Value:", "", 32,TextField.ANY);

private RecordStore recStore;

private Alert success;

public PreferenceStore(){
inputForm = new Form("Preference");

exit = new Command("Exit",Command.EXIT, 0);
write = new Command("Write", Command.OK, 0);
read = new Command("Read", Command.OK, 0);
back = new Command("Back", Command.BACK, 0);

output.addCommand(back);
output.setCommandListener(this);

inputForm.append(varName);
inputForm.append(varValue);
inputForm.addCommand(exit);
inputForm.addCommand(write);
inputForm.addCommand(read);
inputForm.setCommandListener(this);

success = new Alert("","Successfully Added!", null, AlertType.INFO);

}

public void startApp() {
try {
recStore = RecordStore.openRecordStore("Preferences", true);
} catch (RecordStoreException ex) {
ex.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
if(display == null){
display = Display.getDisplay(this);
}
display.setCurrent(inputForm);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d){
if(c == exit){
notifyDestroyed();
try{
recStore.closeRecordStore();
}catch(Exception e){e.printStackTrace();}
}
else if(c == write){
display.setCurrent(success);
writeString(recStore, varName.getString(), varValue.getString());
varName.setString("");
varValue.setString("");
}
else if(c == read){
output.setString("");
output.setString(readVar(recStore, varName.getString(), varValue.getString()));
display.setCurrent(output);
varName.setString("");
varValue.setString("");
}
else if(c == back){
display.setCurrent(inputForm);
}
}

public void writeString(RecordStore recStore, String name, String value){
String newItem = new String(name + value);

byte[] bytes = newItem.getBytes();
try{
recStore.addRecord(bytes, 0, bytes.length);
}catch(Exception e){ e.printStackTrace();}
}

public String readVar(RecordStore recStore, String name, String defaultValue){
StringBuffer sb = new StringBuffer();

try{
RecordEnumeration enumerator = recStore.enumerateRecords(null, null, true);
while(enumerator.hasNextElement()){
String item = new String(enumerator.nextRecord());
String val = new String(name + defaultValue);
if(item.equals(val))
sb.append(item);
}

}catch(Exception e){e.printStackTrace();}

return sb.toString();
}

}

Read More ..

How to View Total and Free Memory in JAVA

This code snippet allows you to view the total memory and free memory in your console at runtime.

public class MemoryViewing {

public static void main(String[] args) {

System.out.println("Total Memory: " + Runtime.getRuntime().totalMemory());

System.out.println("Free Memory: " + Runtime.getRuntime().freeMemory());

}
}


Runtime is a singleton.

Read More ..