2011-10-11 4 views
1

응용 프로그램을 다시 연 후 파일에 객체의 arraylist를 저장하여 저장하려고합니다.파일에 직렬화 된 객체의 arraylist를 저장하는 안드로이드

public class SmsMessage implements Serializable { 

    public static enum MessageType { 
     Sent, 
     Received; 
    }; 

    private String body; 
    private Date date; 
    private MessageType type; 

    public SmsMessage(String _body, Date _date, MessageType _type) { 
     body = _body; 
     date = _date; 
     type = _type; 

    } 

} 전체 클래스의

.

FileOutputStream fout = null; 
ObjectOutputStream out = null; 
    try { 
     fout = context.getApplicationContext() 
        .openFileOutput(FILENAME, Context.MODE_PRIVATE); 
     out = new ObjectOutputStream(fout); 
     out.writeObject(list); 
     out.close(); 

    } catch (IOException ioe) { 
     System.out.println("Error in save method"); 

    } finally { 
     out.close(); 
     fout.close(); 
} 

과 같이 읽어 : :이처럼 저장하고있어

ObjectInputStream in = null; 
FileInputStream fis = null; 
    try { 
     fis = context.getApplicationContext().openFileInput(FILENAME); 
     in = new ObjectInputStream(fis); 
     ArrayList<SmsMessage> list = null; 
     list = (ArrayList<SmsMessage>)in.readObject(); 
    } catch (Exception ex) { 
     System.out.println("Error in get method"); 
    } finally { 
     in.close(); 
     fis.close(); 
} 

이 코드가 작동하지 않습니다 - 내가 전체 ArrayList에 저장있을 때 의미와 응용 프로그램을 죽이고, 응용 프로그램이 열릴 때 다시 읽으려고하면 아무 것도 반환하지 않습니다. 여기 뭐가 잘못 됐니? 이런

답변

0

초기화 :

fout=new FileOutputStream(FILENAME)); 

후 out.writeObject (목록); similarlly이 R 유 로그 캣에 오류 메시지가 표시 .... 작동

 fis = new FileInputStream((FILENAME)); 

희망 초기화이 줄

 out.flush(); 

을 추가 ??

관련 문제