j2me版的Preferences

来源:互联网 发布:佰诺网络 编辑:程序博客网 时间:2024/04/29 13:18
  1. package com.eshore.sweetop.preferences;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import javax.microedition.rms.RecordEnumeration;
  8. import javax.microedition.rms.RecordStore;
  9. import javax.microedition.rms.RecordStoreException;
  10. import javax.microedition.rms.RecordStoreFullException;
  11. import javax.microedition.rms.RecordStoreNotFoundException;
  12. import javax.microedition.rms.RecordStoreNotOpenException;
  13. /**
  14.  * Class Preferences
  15.  * 
  16.  * @author wudongdong Preferences.java Dec 4, 2008
  17.  */
  18. public class Preferences {
  19.     private Class c;
  20.     private RecordStore rs;
  21.     public Preferences(Class c) {
  22.         this.c = c;
  23.         try {
  24.             rs = RecordStore.openRecordStore(c.getName(), true);
  25.         } catch (RecordStoreFullException e) {
  26.             // TODO Auto-generated catch block
  27.             e.printStackTrace();
  28.         } catch (RecordStoreNotFoundException e) {
  29.             // TODO Auto-generated catch block
  30.             e.printStackTrace();
  31.         } catch (RecordStoreException e) {
  32.             // TODO Auto-generated catch block
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.     public static Preferences userNode(Class c) {
  37.         return new Preferences(c);
  38.     }
  39.     public void put(String key, String value) {
  40.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  41.         DataOutputStream dos = new DataOutputStream(baos);
  42.         try {
  43.             dos.writeUTF(key);
  44.             dos.writeUTF(value);
  45.             baos.close();
  46.             dos.close();
  47.         } catch (IOException e) {
  48.             e.printStackTrace();
  49.         }
  50.         byte[] b = baos.toByteArray();
  51.         try {
  52.             int id=id(key);
  53.             if(id==-1){
  54.                 rs.addRecord(b, 0, b.length);
  55.             }else{
  56.                 rs.setRecord(id, b, 0, b.length);
  57.             }
  58.         } catch (RecordStoreNotOpenException e) {
  59.             e.printStackTrace();
  60.         } catch (RecordStoreFullException e) {
  61.             e.printStackTrace();
  62.         } catch (RecordStoreException e) {
  63.             e.printStackTrace();
  64.         }
  65.     }
  66.     public String get(String key) {
  67.         RecordEnumeration re = null;
  68.         try {
  69.             re = rs.enumerateRecords(nullnullfalse);
  70.             while (re.hasNextElement()) {
  71.                 byte[] b = re.nextRecord();
  72.                 ByteArrayInputStream bais = new ByteArrayInputStream(b);
  73.                 DataInputStream dis = new DataInputStream(bais);
  74.                 String k = dis.readUTF();
  75.                 if (key.equals(k)) {
  76.                     return dis.readUTF();
  77.                 }
  78.             }
  79.         } catch (Exception e) {
  80.             e.printStackTrace();
  81.         } finally {
  82.             if (re != null) {
  83.                 re.destroy();
  84.             }
  85.         }
  86.         return null;
  87.     }
  88.     
  89.     private int id(String key) {
  90.         RecordEnumeration re = null;
  91.         try {
  92.             re = rs.enumerateRecords(nullnullfalse);
  93.             while (re.hasNextElement()) {
  94.                 int id=re.nextRecordId();
  95.                 byte[] b = rs.getRecord(id);
  96.                 ByteArrayInputStream bais = new ByteArrayInputStream(b);
  97.                 DataInputStream dis = new DataInputStream(bais);
  98.                 String k = dis.readUTF();
  99.                 if (key.equals(k)) {
  100.                     return id;
  101.                 }
  102.             }
  103.         } catch (Exception e) {
  104.             e.printStackTrace();
  105.         } finally {
  106.             if (re != null) {
  107.                 re.destroy();
  108.             }
  109.         }
  110.         return -1;
  111.     }   
  112.     
  113.     public void dispose(){
  114.         try {
  115.             rs.closeRecordStore();
  116.         } catch (RecordStoreNotOpenException e) {
  117.             // TODO Auto-generated catch block
  118.             e.printStackTrace();
  119.         } catch (RecordStoreException e) {
  120.             // TODO Auto-generated catch block
  121.             e.printStackTrace();
  122.         }
  123.     }
  124. }
原创粉丝点击