【问题标题】:read/write an object to file读/写一个对象到文件
【发布时间】:2013-08-12 18:02:06
【问题描述】:

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

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

【问题讨论】:

  • 文件系统中没有文件。首先创建一个文件然后打开它。

标签: android serialization filenotfoundexception


【解决方案1】:

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

编辑:这是代码的长版本

阅读:

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

【讨论】:

  • 我只需要将它存储在内部目录中。
  • 你只需要使用 getFilesDir() 而不是 Environment.getExternalStorageDirectory()
  • 我似乎无法让它工作。在我的无效设置中,我正在阅读它。 (无效设置只运行一次)。在我的 void draw 中,我正在检查触摸,如果为真,它将一个对象写入文件。下次我运行应用程序时,在 void setup 中读取的值仍然是 0。我不知道是什么问题。
  • 另外,这个(尚未)保存的文件的位置是什么?
  • 它在数据 > 数据 > app.package.name > 文件中
【解决方案2】:

FileNotFoundException 在创建新的FileOutputStream 时表示其中一个中间目录不存在。试试

file.getParentFile().mkdirs();

在创建FileOutputStream.之前

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    相关资源
    最近更新 更多