2013-08-11 8 views
9

hier ist der Code: meine Mission ist es, ein Objekt (Person) zu serialisieren, speichern Sie es in einer Datei in Android (privat), lesen Sie die Datei später, (ich werde ein Byte bekommen Array) und deserialize das byta-Array.lesen/schreiben ein Objekt in Datei

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

    } 

und meine Funktion:

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









    } 

es eine FileNotFoundException zurückkehrt.

(i bin hier, so wenden Sie sich bitte geduldig und verständnisvoll sein)

EDIT :: das ist, wie ich gelesen habe (versuchen zu), (für 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(); 
    } 

und zum Lesen :::

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

Erstellen Sie keine Datei im Dateisystem haben. Erstellen Sie zuerst eine Datei und öffnen Sie sie dann. – Ashwani

Antwort

14

Sie müssen nicht den 'Byte-Array'-Ansatz verwenden. Es gibt eine einfache Möglichkeit, Objekte zu (de) serialisieren.

EDIT: Hier ist die lange Version von Code

lesen:

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

} 

schreiben:

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 Klasse:

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

Ich muss es nur im internen Verzeichnis speichern. –

+0

siehe aktualisierte Antwort – growic

+0

Sie müssen nur getFilesDir() anstelle von Environment.getExternalStorageDirectory() – growic

0

FileNotFoundException beim Erstellen eines neuen FileOutputStream bedeutet, dass eines der Zwischenverzeichnisse nicht vorhanden war. Versuchen

file.getParentFile().mkdirs(); 

vor dem FileOutputStream.

Verwandte Themen