import java.io.*;
import javax.microedition.lcdui.List;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class DataOperate {
RecordStore rs;
int count;
public DataOperate()
{
try {
rs = RecordStore.openRecordStore("Phone", true);
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void save(String str)
{
try {
rs = RecordStore.openRecordStore("Phone", true);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
dos.writeUTF(str);
dos.close();
byte[] b=baos.toByteArray();
rs.addRecord(b, 0, b.length);
} catch (RecordStoreNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RecordStoreFullException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RecordStoreNotOpenException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (RecordStoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void delete(String str)
{
try
{
RecordEnumeration re=rs.enumerateRecords(null, null, false);
while(true)
{
int id=re.nextRecordId();
byte[] b=rs.getRecord(id);
ByteArrayInputStream bais=new ByteArrayInputStream(b);
DataInputStream dis=new DataInputStream(bais);
String temp=dis.readUTF();
if(temp.endsWith(str))
{
rs.deleteRecord(id);
break;
}
}
}
catch(RecordStoreNotFoundException e)
{
e.printStackTrace();
}
catch(RecordStoreException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public List get()
{
List list=null;
try {
rs = RecordStore.openRecordStore("Phone", true);
RecordEnumeration re;
re = rs.enumerateRecords(null, null, false);
count=re.numRecords();
String str[]=new String[count];
for(int i=0;i<count;i++)
{
byte[] b=re.nextRecord();
ByteArrayInputStream bais=new ByteArrayInputStream(b);
DataInputStream dis=new DataInputStream(bais);
String temp=dis.readUTF();
str[i]=temp;
}
list=new List("备忘录信息",List.IMPLICIT,str,null);
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(InvalidRecordIDException e)
{
e.printStackTrace();
}catch(RecordStoreException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
return list;
}
}