Android File I/O를 살펴본 이유는 POJO 를 저장하기 위해서다. 

사실 SharedPreference를 통해서 POJO를 저장할 수 있길 기대 했지만, SharedPreference는 Java Object를 저장할 수 없게 되어 있다.

그래서 결국 POJO를 Serialize 해서, File로 떨구기로 결정했기에, Android File I/O에 대해서 살펴 보기로 하였다. 

Android에서 File I/O의 Target Directory는 두가지일수 있다. 
하나는 Internal Storage이고, 다른 하나는 External Stroage(SD Card)이다. 
POJO를 저장하는 건 보통 매우 작은 단위의 Data이기 때문에, Internal Storage로 결정했다.

File I/O 역시, Context Class를 통해서 해결한다. 
Context Class를 살펴 보면 openFileInput/openFileOutput이 있다. 

abstract FileInputStream openFileInput(String name)
Open a private file associated with this Context's application package for reading.
abstract FileOutputStream openFileOutput(String name, int mode)
Open a private file associated with this Context's application package for writing.

보통 Java Application 에서 FileInputStream을 직접 사용하는것과 다리 
Context의 openFileInput Method를 통해서 internal Storage에서 해당 App의 SandBox Directory로 부터 File Stream을 열수 있다. 

그외에 FileXXXXStream을 사용하는 방법은 일반 Java Programming과 동일 함으로 생략.

여기서 한가지 더 필요 했던게, 만든 파일을 어떻게 지울것인가 이다. 

지우는 Method도 Context Class에 있다. 

abstract boolean deleteFile(String name)
Delete the given private file associated with this Context's application package.
뭐 역시나 별로 어려운건 없다 ㅎ

+ Recent posts