2014-02-09 7 views
0

내가 만든 클래스 유형의 배열 목록이 있습니다. 필자는 objectoutputstream을 사용하여 배열 목록을 파일에 작성했습니다. 이제는 객체로 다시 읽었지만, 어떻게 다시 arraylist로 가져갈 수 있습니까? 이 ArrayList에 저장 한 경우사용자 정의 클래스의 객체 Arraylist

//Open external storage 
     File root = android.os.Environment.getExternalStorageDirectory(); 
     //Get directory path 
     File dir = new File(root.getAbsolutePath() + "/download"); 
     File file = new File(dir, "userAccounts.txt"); 

     try{ 
      FileInputStream fis = new FileInputStream(file); 
      ObjectInputStream ois = new ObjectInputStream(fis); 
      UserAccounts tempUser = (UserAccounts) ois.readObject(); 
      Global.getInstance().userArray.add(tempUser); 
      ois.close(); 
      Toast.makeText(getApplicationContext(), Global.getInstance().userArray.get(0).aUsername, Toast.LENGTH_LONG).show(); 

      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

클래스

package com.example.cg4project; 

import java.io.Serializable; 

public class UserAccounts implements Serializable{ 
    /** 
    * 
    */ 

    public int aID; 
    public String aUsername; 
    public String aFirstName; 
    public String aLastName; 
    public String aemail; 
v 

답변

0

,이처럼 다시 읽을 수 있습니다

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); 
ArrayList<UserAccounts> myArray = (ArrayList<UserAccounts>) ois.readObject(); 

Global.getInstance().userArray.clear(); 
Global.getInstance().userArray.addAll(myArray); 
+0

을 내 ArrayList의 글로벌 ArrayList의 경우 내가 저장 가겠어요 방법 그것에서 그 – xiimoss

+0

나는 당신이 당신의 글로벌 배열에 결과를 넣을 방법을 보여주는 나의 대답을 업데이트했다 – brwngrldev

+0

정말 고마워! – xiimoss