hi am trying to write some code for serailizing objects from an array to rms and deserialize them back out. the problem is that it only read the second item twice. heres my code:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
import java.io.*;
public class serialTest extends MIDlet
{
User users[] = new User[2];
RecordStore rs = null;
public serialTest() { }
public class User {
private String password;
private String name;
public User(){
this(null,null);
}
public User(String name, String password){
this.name = name;
this.password = password;
}
public String getName() {return name;}
public String getPassword() {return password;}
// rms methods
public synchronized byte[] serialize()
{
try
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeUTF(name != null ? name : "");
dout.writeUTF(password != null ? password : "");
return bout.toByteArray();
}
catch (Exception e) {return null;}
}
public synchronized boolean deserialize(byte[] data)
{
try
{
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);
name = din.readUTF();
password = din.readUTF();
return true;
}
catch (Exception e) {return false;}
}
}
protected void startApp() throws MIDletStateChangeException
{
users[0] = new User("Bob", "qwerty");
users[1] = new User("Steve", "asdfghjk");
try
{
RecordStore.deleteRecordStore("Users");
rs = RecordStore.openRecordStore("Users", true);
try
{
System.out.println(rs.getNumRecords());
byte[] data;
for(int i=0;i<users.length;i++)
{
System.out.println("adding record");
data = users[i].serialize();
rs.addRecord(data, 0, data.length);
}
System.out.println(rs.getNumRecords());
rs.closeRecordStore();
} catch(RecordStoreFullException rsfe){ }
} catch(RecordStoreException rse){ }
try
{
System.out.println("opened recordstore");
rs = RecordStore.openRecordStore("Users", false);
System.out.println(rs.getNumRecords());
try
{
byte[] data;
for(int i=0;i<rs.getNumRecords();i++)
{
System.out.println("reading record");
data = rs.getRecord(i+1);
users[i].deserialize(data);
}
rs.closeRecordStore();
} catch(RecordStoreException rse){ }
} catch(RecordStoreException rse){ }
for(int i=0;i<2;i++)
{
System.out.println("*****check return methods*******");
System.out.println("name = " + users[i].getName());
System.out.println("password = " + users[i].getPassword());
}
}
protected void destroyApp(boolean unconditional) { }
protected void pauseApp() { }
}
any help would be apprecited thanks.

Reply With Quote

