Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read/write an object to file

here is the code : my mission is to serialize an my object(Person) , save it in a file in android(privately), read the file later,(i will get a byte array), and deserialize the byta array.

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

    }

and my function :

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









    }

it is returning a filenotfoundexception .

(i am new at this, so please be patient and understanding)

EDIT ::this is how i am (trying to ) read, (for cerntainly)

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

and for reading :::

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();
    }
}
like image 611
harveyslash Avatar asked Aug 11 '13 07:08

harveyslash


1 Answers

You don't need to use the 'byte array' approach. There is an easy way to (de)serialize objects.

EDIT: here's the long version of code

Read:

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

}

Write:

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

Person class:

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;
    }
}
like image 53
growic Avatar answered Oct 24 '22 13:10

growic