课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——J2ME中对象序列化的方法


  共有15585人关注过本帖树形打印复制链接

主题:程序代码——J2ME中对象序列化的方法

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——J2ME中对象序列化的方法  发帖心情 Post By:2008/10/21 21:16:31 [只看该作者]

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.Date;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class Exec extends MIDlet {
    private Display display;
    Word word;
    Word[] words;
    private Form mainForm;
    
    public Exec() {
        mainForm = new Form("RMS基本操作");
        handlingRMS();
        for (int i = 0; i < words.length; i++) {
            mainForm.append(words.toString()+"\n");
        }
        display = Display.getDisplay(this);
    }
    
    public void startApp() {
        display.setCurrent(mainForm);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    public void handlingRMS() {
        try {
            RecordStore rs = null;
            rs = RecordStore.openRecordStore("WordsRMS", true);
            word = new Word("NULL", "NULL", (new Date()).getTime(), "NULL");
            byte[] data = word.serialize();
            rs.addRecord(data, 0, data.length);
            rs.closeRecordStore();
            
            rs = RecordStore.openRecordStore("WordsRMS", false);
            RecordEnumeration re = rs.enumerateRecords(null, null, false);
            words = new Word[re.numRecords()];
            
            int i = 0;
            while (re.hasNextElement()) {
                byte[] tmp = re.nextRecord();
                words = Word.deserialize(tmp);
                i++;
            }
            rs.closeRecordStore();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

class Word {
    private String enWord;
    private String cnWord;
    private long dateTime;
    private String detail;
    
    public Word() {
    }
    
    public Word(String enWord, String cnWord, long dateTime, String detail) {
        setEnWord(enWord);
        setCnWord(cnWord);
        setDateTime(dateTime);
        setDetail(detail);
    }
    
    public String getEnWord() {
        return enWord;
    }
    
    public void setEnWord(String enWord) {
        this.enWord = enWord;
    }
    
    public String getCnWord() {
        return cnWord;
    }
    
    public void setCnWord(String cnWord) {
        this.cnWord = cnWord;
    }
    
    public long getDateTime() {
        return dateTime;
    }
    
    public void setDateTime(long dateTime) {
        this.dateTime = dateTime;
    }
    
    public String getDetail() {
        return detail;
    }
    
    public void setDetail(String detail) {
        this.detail = detail;
    }
    
    public byte[] serialize() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            DataOutputStream dos = new DataOutputStream(baos);
            dos.writeUTF(this.enWord);
            dos.writeUTF(this.cnWord);
            dos.writeLong(this.dateTime);
            dos.writeUTF(this.detail);
            baos.close();
            dos.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return baos.toByteArray();
    }
    
    public static Word deserialize(byte[] data) {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);
        Word word = new Word();
        try {
            word.enWord = dis.readUTF();
            word.cnWord = dis.readUTF();
            word.dateTime = dis.readLong();
            word.detail = dis.readUTF();
            bais.close();
            dis.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return word;
    }
    
    public String toString() {
        return enWord + ":" + cnWord + ":" + new Date(dateTime) + ":" + detail;
    }
}


 

[此贴子已经被作者于2010-12-12 18:38:38编辑过]

 回到顶部