10

这是代码:我的任务是序列化我的对象(Person),将其保存在 android 的文件中(私下),稍后读取文件,(我将获得一个字节数组),然后反序列化 byta 数组。

       public void setup()
    {

           byte[] data = SerializationUtils.serialize(f);


             WriteByteToFile(data,filename); 



    }
Person p =null ;
    public void draw()
    {
        File te = new File(filename);
         FileInputStream fin = null;


             try {
                fin=new FileInputStream(te);
                byte filecon[]=new byte[(int)te.length()];
                fin.read(filecon);
                String s = new String(filecon);
                System.out.println("File content: " + s);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }






        text(p.a,150,150);

    }

和我的功能:

public void WriteByteToFile(byte[] mybytes, String filename){

        try {

        FileOutputStream FOS = openFileOutput(filename, MODE_PRIVATE);
        FOS.write(mybytes);
        FOS.close();


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("done");









    }

它返回一个 filenotfoundexception 。

(我是新手,所以请耐心等待)

编辑::这就是我(试图)阅读的方式,(当然)

ObjectInputStream input = null;
    String filename = "testFilemost.srl";
    try {
        input = new ObjectInputStream(new FileInputStream(new File(new File(getFilesDir(),"")+File.separator+filename)));
    } catch (StreamCorruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        Person myPersonObject = (Person) input.readObject();
        text(myPersonObject.a,150,150);
    } catch (OptionalDataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        input.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

和阅读:::

if(mousePressed)

{
    Person myPersonObject = new Person();
    myPersonObject.a=432;
    String filename = "testFilemost.srl";
    ObjectOutput out = null;

    try {
        out = new ObjectOutputStream(new FileOutputStream(new File(getFilesDir(),"")+File.separator+filename));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        out.writeObject(myPersonObject);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        out.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
4

2 回答 2

19

您不需要使用“字节数组”方法。有一种简单的方法可以(反)序列化对象。

编辑:这是代码的长版本

读:

public void read(){
    ObjectInputStream input;
    String filename = "testFilemost.srl";

    try {
        input = new ObjectInputStream(new FileInputStream(new File(new File(getFilesDir(),"")+File.separator+filename)));
        Person myPersonObject = (Person) input.readObject();
        Log.v("serialization","Person a="+myPersonObject.getA());
        input.close();
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

写:

public void write(){
    Person myPersonObject = new Person();
    myPersonObject.setA(432);
    String filename = "testFilemost.srl";
    ObjectOutput out = null;

    try {
        out = new ObjectOutputStream(new FileOutputStream(new File(getFilesDir(),"")+File.separator+filename));
        out.writeObject(myPersonObject);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

人物类:

public class Person implements Serializable {
    private static final long serialVersionUID = -29238982928391L;
    int a;

    public int getA(){
        return a;
    }

    public void setA(int newA){
        a = newA;
    }
}
于 2013-08-11T08:07:15.580 回答
0

FileNotFoundException创建新时FileOutputStream意味着中间目录之一不存在。尝试

file.getParentFile().mkdirs();

在创建之前FileOutputStream.

于 2013-08-14T08:39:55.717 回答