Vou postar aqui um exemplo de como usar RMS para guardar um objeto, as informação vieram desses links
interface Persistent
import java.io.*;
/*interface para construir persistência de objetos onde
* não é possível fazer serialização, no caso J2ME*/
public interface Persistent {
byte[] persist() throws IOException;
void resurrect( byte[] data ) throws IOException;
}
Classe Employee
[code]
import java.io.*;
/*classe que implementa os métodos da interface Presistent
*de forma a poder pegar o valor do objeto após receber um tipo byte[]
que está armazenado do RMS/
public class Employee implements Persistent {
private int employeeID;
private String firstName;
private String lastName;
private int managerID;
public Employee(){
}
public Employee( int employeeID, String firstName,
String lastName, int managerID ){
this.employeeID = employeeID;
this.firstName = firstName;
this.lastName = lastName;
this.managerID = managerID;
}
public int getID() { return employeeID; }
public String getFirstName() {
return firstName != null ? firstName : "";
}
public String getLastName() {
return lastName != null ? lastName : "";
}
public int getManagerID() { return managerID; }
public String toString() {
StringBuffer b = new StringBuffer();
b.append( '{' );
b.append( employeeID );
b.append( ',' );
b.append( firstName );
b.append( ',' );
b.append( lastName );
b.append( ',' );
b.append( managerID );
b.append( '}' );
return b.toString();
}
public byte[] persist() throws IOException {
ByteArrayOutputStream bout =
new ByteArrayOutputStream();
DataOutputStream dout =
new DataOutputStream( bout );
dout.writeInt( getID() );
dout.writeUTF( getFirstName() );
dout.writeUTF( getLastName() );
dout.writeInt( getManagerID() );
dout.flush();
return bout.toByteArray();
}
public void resurrect( byte[] data )
throws IOException {
ByteArrayInputStream bin =
new ByteArrayInputStream( data );
DataInputStream din =
new DataInputStream( bin );
employeeID = din.readInt();
firstName = din.readUTF();
lastName = din.readUTF();
managerID = din.readInt();
}
}[/code]
Gravando e pegando um objeto em um RMS
[code]
import java.io.IOException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
public class Rms extends MIDlet{
public void startApp() {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("FTut", true);
// Gravando uma informação
Employee empregado = new Employee(1,"Bruno","Bastos",2);
Runtime.getRuntime().freeMemory();
byte [] dados = empregado.persist();
int id = rs.addRecord(dados, 0, dados.length);
dados = rs.getRecord(id);
empregado.resurrect(dados);
System.out.println(empregado);
} catch(RecordStoreException rse) {
rse.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.closeRecordStore();
}
} catch(RecordStoreException rse) {
rse.printStackTrace();
}
}
}
protected void pauseApp() {
// TODO implementar
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO implementar
}
}[/code]
Depois de fazer esse exemplo deu para ter uma noção boa, do que fazer para trabalhar com um arquivo RMS