xyh9717 2011-05-31 21:47
浏览 332
已采纳

java数组序列化后存储到文件,读出来的数据和写入的不一样


import java.io.Serializable;
public class Map implements Serializable{
private byte[][] map;

public byte[][] getMap() {
    return map;
}
public void setMap(byte[][] map) {
    this.map = map;
}

}


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Vector;

public class ControlMap {

Vector<Map> mapVector=new Vector<Map>();

public void writeMap(byte myMap[][])
{
    readMap();
    ObjectOutputStream oos;
    try {
        oos = new ObjectOutputStream(new FileOutputStream("map.map"));
        Map map=new Map();
        map.setMap(myMap);
        mapVector.add(map);
        oos.writeObject(mapVector);
        oos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void readMap()
{
    try {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("map.map"));
        mapVector=(Vector)(ois.readObject());
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


public String toString()
{
    readMap();
    int size=mapVector.size();
    String temp="";
    for(int i=0;i<size;i++)
    {
        temp+="第"+(i+1)+"张地图:\n";
        Map map=mapVector.get(i);
        byte mapData[][]=map.getMap();
        for(int j=0;j<mapData.length;j++)
            temp+=Arrays.toString(mapData[i])+"\n";
        temp+="\n";
    }
    return temp;
}

}


public class Test {

public static void main(String[] args) {
    ControlMap control=new ControlMap();
    byte [][] myMap={
            {3,7,5,2,4,6,8},
            {3,7,5,2,4,6,8},
            {3,7,5,2,4,6,8},
            {3,7,5,2,4,6,8},
            {3,7,5,2,4,6,8},
            {3,7,5,2,4,6,8},
            {3,7,5,2,4,6,8}
    };
    control.writeMap(myMap);
    System.out.println(control);
}

}


  • 写回答

2条回答

  • heihei0923 2011-06-01 00:18
    关注

    public String toString()
    {
    readMap();
    int size=mapVector.size();
    String temp="";
    for(int i=0;i<size;i++)
    {
    temp+="第"+(i+1)+"张地图:\n";
    Map map=mapVector.get(i);
    byte mapData[][]=map.getMap();
    for(int j=0;j<mapData.length;j++)
    temp+=Arrays.toString(mapData[[color=red]i[/color]])+"\n";
    temp+="\n";
    }
    return temp;
    }
    这个是不是该改成j 。大哥。呵呵

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable