2013-05-09 88 views
0

我遇到以下問題。我正在設計手寫應用程序。 我不知道如何寫入和讀取我的對象(LogInfo)到文件中。我知道我應該重寫寫入和讀取方法,但我仍然不確定如何去做。任何人都可以給我明確的答案嗎?將自定義對象寫入並讀取文件android

我類如下:

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); 
     } 
    } 
} 

筆畫是包含指向型的2維陣列的ArrayList(對應於與它相關的筆劃數和分)

代碼是其中一個陣列我存儲字符(3個字節= 1個字符)

因此,如果我使用2個筆劃寫入「A」,筆劃和代碼大小爲2,並且代碼包含寫入3個字節的字符A.

有人能告訴我怎麼寫和讀這個對象?

回答

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); 
    } 
} 

它對我很好。

0

由於Point不可序列化。你可以有一個自定義的「點」類和一個從它派生的瞬態點。這樣,你可以使用默認的readObject和writeObject。

Check this out

+0

我認爲Point類不是Serializable。 – Marek 2013-05-09 01:16:10

+0

另外,當我使用默認實現時,寫入文件的唯一信息是類路徑和一些隨機字符([email protected] xp),肯定不是它應該是的信息。爲什麼有一個類名寫入文件? – Marek 2013-05-09 01:25:40

+0

沒有人知道答案? :( – Marek 2013-05-09 04:08:16

相關問題