2013-08-11 39 views
9

đây là mã: Nhiệm vụ của tôi là sắp xếp một đối tượng của tôi (Person), lưu nó vào một tệp trong android (riêng tư), đọc tệp sau, (tôi sẽ nhận được byte mảng) và deserialize mảng byta.đọc/ghi một đối tượng vào tệp

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

    } 

và chức năng của tôi:

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









    } 

nó được trả lại một FileNotFoundException.

(i am mới tại này, vì vậy hãy kiên nhẫn và hiểu biết)

EDIT :: đây là cách tôi đang (cố gắng) đọc, (ví 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(); 
    } 

và đọc :::

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

bạn không có tập tin trong hệ thống tập tin. Trước tiên hãy tạo một tệp rồi mở tệp đó. – Ashwani

Trả lời

14

Bạn không cần sử dụng phương pháp 'mảng byte'. Có một cách dễ dàng để (de) serialize đối tượng.

EDIT: đây là phiên bản dài của mã

đọc:

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

} 

Viết:

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 lớp:

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

tôi chỉ cần lưu trữ nó trong thư mục nội bộ. –

+0

xem câu trả lời cập nhật – growic

+0

Bạn chỉ cần sử dụng getFilesDir() thay vì Environment.getExternalStorageDirectory() – growic

0

FileNotFoundException khi tạo một FileOutputStream mới có nghĩa là một trong các thư mục trung gian không tồn tại. Hãy thử

file.getParentFile().mkdirs(); 

trước khi tạo FileOutputStream.

Các vấn đề liên quan