2012-06-29 2 views
0

사용자가 ID 집합 (Federation ID)을 선택하여 외부 API에서 데이터를 검색 할 수있는 시스템을 만들려고합니다. 데이터가 웹 서비스에서 검색된 후에 나중에 사용할 수 있도록 로컬로 저장하려고합니다. 이제는 전체 파일 (federations.dat)을 가져 와서 ObjectInputStream에서 객체를로드하는 방법을 알고 있습니다. 나에게 "federations.dat"에서 "object WHERE id = N"이라는 말을로드 할 수있는 방법이 있습니까? 아니면 각 개체에 대해 별도의 파일을 만들어야합니까?하나의 파일에서 여러 객체를 저장하려면 어떻게해야합니까?

이 내 부하 방법 :

public static Object load(Context ctx, String filename) throws FileNotFoundException 
{ 
    Object loadedObj = null; 
    InputStream instream = null; 

    instream = ctx.openFileInput(filename); 

    try { 
     ObjectInputStream ois = new ObjectInputStream(instream); 
     loadedObj = ois.readObject(); 
     return loadedObj; 

    } catch (StreamCorruptedException e) { 
     e.printStackTrace(); 
     return null; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

어떤 제안이 마음에 와서?

+0

,이 열려 OIS 잎이 데이터베이스 http://developer.android.com/guide/topics/data/data-storage.html#db –

답변

1

확장자를 @Jan의 코드를 오픈 ois을 유지하는 문제를 해결 몇개의 사소한 문제와 함께, 예외가 슬로우되었을 경우. 내가 잘못 본게 아니라면

안드로이드 응용 프로그램에서
public static ArrayList<Object> load(Context ctx, String filename) throws FileNotFoundException { 
    InputStream instream = ctx.openFileInput(filename); 

    ArrayList<Object> objects = new ArrayList<Object>(); 

    try { 
     ObjectInputStream ois = new ObjectInputStream(instream); 
     try{ 
      Object loadedObj = null; 
      while ((loadedObj = ois.readObject()) != null) { 
       objects.add(loadedObj); 
      } 

      return objects; 
     }finally{ 
      ois.close(); 
     } 

    } catch (StreamCorruptedException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
2

당신은 다음과 같이 사용할 수 있습니다 ..

ArrayList<Object> arrayList = new ArrayList<Object>(); 

Object obj = null; 

while ((obj = ois.readObject()) != null) { 
    arrayList.add(obj); 
} 

당신은 당신의 방법에 ArrayList를 반환 할 수 있습니다.

return arrayList; 

편집 : 전체 코드는 다음과 같을 것이다 ..

public static ArrayList<Object> load(Context ctx, String filename) 
{ 
    InputStream fis = null; 
    ObjectInputStream ois = null; 

    ArrayList<Object> arrayList = new ArrayList<Object>(); 

    Object loadedObj = null; 
    try { 
     fis = ctx.openFileInput(filename); 
     ois = new ObjectInputStream(fis); 

     while ((loadedObj = ois.readObject()) != null) { 
      arrayList.add(loadedObj); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } finally { 
     if (null != ois) ois.close(); 
     if (null != fis) fis.close(); 
    } 

    return arrayList; 
} 

는 희망이 도움이 ..

+1

에 저장할 수 있습니다 예외가 발생했을 때 내 OOP 클래스는 두 개의 중첩 된 시도를 사용하도록 옹호했으며, 바깥 쪽 하나는 예외를 잡아 내고 내부에는 ois 만 닫는 finally가 있습니다. – DZittersteyn

+0

그것은 .. @ litemode의 코드를 편집해야했습니다. 개발자가 해결해야하는 문제입니다. 하지만 어쨌든 좋은 캐치였습니다. – Jan

+0

또한, 처음에는 ois에서 객체를 읽을 때 그 객체는 무시됩니다. – DZittersteyn