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个回答

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 。大哥。呵呵

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[[b]i[/b]])+"\n";
temp+="\n";
}
return temp;
}
这个是不是该改成j 。大哥。呵呵

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问