파일 관리

파일 경로는 /data/data/패키지명/files 디렉토리에 저장

Context 클래스

입출력 관련 메서드

파일 삭제 메서드


파일 저장 예

try {
   FileOutputStream fos = openFileOutput("test.txt", Context.MODE_WORLD_READABLE);
   String str = "Android File IO Test";
   fos.write(str.getBytes());
   fos.close();
   mEdit.setText("write success");
} catch (FileNotFoundException e) {
   System.out.println(e.getMessage());
} catch (IOException e) {
   System.out.println(e.getMessage());
}

openFileOutput() 메서드를 이용. String name : 파일명, int mode : 모드 선택

write() 메서드를 이용해서 파일 안에 데이터를 저장한다. 마지막엔 close()를 호출한다.

파일을 삭제할 때는 deleteFile(String name) 메서드를 호출하면 된다.

res 디렉토리에서 파일을 불러올 때는 위에 메서드들을 사용한다.


InputStream fres = getResources().openRawResource(R.raw.restext);

이런 형식으로

Comment are closed.