写入和读取自定义对象到文件android

时间:2013-05-09 00:45:16

标签: android file object

我有以下问题。我正在设计手写应用程序。 我不知道如何编写和读取我的对象(LogInfo)到文件中。我知道我应该覆盖写入和读取方法,但我仍然不确定如何做到这一点。有人能给我一个明确的答案吗?

我的课程如下:

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.graphics.Point;

public class LogInfo implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -5777674941129067422L;
    transient public static List<Point[][]> strokes;
    transient public static List<byte[]> codes;

    public LogInfo()
    {
        strokes = new ArrayList<Point[][]>();
        codes = new LinkedList<byte[]>();
    }

    private synchronized void writeObject(ObjectOutputStream stream) throws IOException {
        stream.defaultWriteObject();
        //stream.writeInt(strokes.size());
        //Point[][] pointsArray;
        //for (int i=0; i<strokes.size(); i++)
        //{
        //  pointsArray = ((Point[][])strokes.get(i));
        //  for (int j = 0; j < pointsArray.length; j++)
        //      for (int k = 0; k < pointsArray[j].length; k++)
        //      {
        //          stream.writeInt(pointsArray[j][k].x);
        //          stream.writeInt(pointsArray[j][k].y);
        //          //stream.writeObject(elementData[i]);
        //      }
        //}
        stream.writeInt(codes.size());
        for (int i=0; i<codes.size(); i++)
        {
            stream.write(codes.get(i));
        }
    }

    private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException
    {
        stream.defaultReadObject();
        int codesSize = stream.readInt();
        for (int i=0; i<codesSize; i++)
        {
            byte[] buffer = null;
            stream.read(buffer, 0, 3);
            codes.add(buffer);
        }
    }
}

Strokes是一个ArrayList,包含Point类型的二维数组(对应于笔画编号和与之相关的点)

代码是一个数组,我在其中存储字符(3个字节= 1个字符)

因此,如果我使用2个笔划写“A”,笔画和代码大小为2,代码包含3个字节写的字符A.

有人能告诉我怎样才能写和阅读这些物品?

2 个答案:

答案 0 :(得分:0)

由于Point不可序列化。您可以使用x&amp ;;定制“点”类。 y和从中导出的瞬态点。这样,您可以使用默认的readObject和writeObject。

Check this out

答案 1 :(得分:0)

我想我找到了解决方案。

private void writeObject(ObjectOutputStream stream) throws IOException {
    stream.defaultWriteObject();
    stream.writeInt(strokes.size());
    Point[][] pointsArray = null;
    for (int i = 0; i < strokes.size(); i++)
    {
        pointsArray = ((Point[][])strokes.get(i));
        stream.writeInt(pointsArray.length);
        for (int j = 0; j < pointsArray.length; j++)
        {
            stream.writeInt(pointsArray[j].length);
            for (int k = 0; k < pointsArray[j].length; k++)
            {
                stream.writeInt(pointsArray[j][k].x);
                stream.writeInt(pointsArray[j][k].y);
                //stream.writeObject(elementData[i]);
            }
        }
    }

    int size = codes.size();
    stream.writeInt(size);
    for (int i = 0; i < size; i++)
    {
        stream.write(codes.get(i));
    }
}

private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException
{
    stream.defaultReadObject();
    int strokesSize = stream.readInt();
    for (int i = 0; i < strokesSize; i++)
    {
        int arrayXSize = stream.readInt();
        Point[][] points = new Point[arrayXSize][];
        for (int j = 0; j < arrayXSize; j++)
        {
            int arrayYSize = stream.readInt();
            points[j] = new Point[arrayYSize];
            for (int k = 0; k < arrayYSize; k++)
                points[j][k] = new Point(stream.readInt(), stream.readInt());
        }
        strokes.add(points);
    }

    int codesSize = stream.readInt();
    for (int i = 0; i < codesSize; i++)
    {
        byte[] buffer = new byte[3];
        stream.read(buffer, 0, 3);
        codes.add(buffer);
    }
}

对我来说效果很好。