Serialization via J2ME or BlackBerry APIs

后端 未结 4 2073
时光说笑
时光说笑 2020-12-21 07:29

Is it possible to serialize an object into a string or a byte array using either the J2ME or BlackBerry APIs?

Thanks.

4条回答
  •  失恋的感觉
    2020-12-21 08:16

    You are stuck with creating your own serialization process for your classes. It wouldn't be too difficult to create your own base class and then use somesort of reflection to automatically serialize your properties.

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
    
        DataOutputStream outputStream = new DataOutputStream(baos);
        try {
            // serialize your object - 
            outputStream.writeInt(this.name);
            // Then push the player name.
            outputStream.writeUTF(this.timestamp);
        }
        catch (IOException ioe) {
            System.out.println(ioe);
            ioe.printStackTrace();
        }
    
    
    // Extract the byte array
    byte[] b = baos.toByteArray();
    

提交回复
热议问题