lecture/écriture à un objet de fichier

voici le code :
ma mission est de sérialiser un objet(une Personne) , l'enregistrer dans un fichier sur android(en privé), de lire le fichier plus tard,(je vais obtenir un tableau d'octets), et désérialiser le byta tableau.

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

    }

et ma fonction :

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









    }

il est de retour d'une filenotfoundexception .

(je suis nouveau à cela, donc s'il vous plaît être patient et compréhensif)

EDIT: c'est de cette façon je suis en train (d'essayer ) de le lire, (pour 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();
    }

et pour la lecture :::

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();
    }
}
vous n'avez pas de fichier dans le système de fichiers. Tout d'abord créer un fichier, puis l'ouvrir.

OriginalL'auteur harveyslash | 2013-08-11