본문 바로가기

Android

ArrayList 파일로 저장 및 읽기

경로 예
this.getExternalFilesDir(null).getPath();
Environment.getExternalStorageDirectory().toString();
/storage/emulated/0/Pictures/원하는 폴더명 등


해당 폴더가 없으면 만들고 저장

public void saveCurrentGame() {
	String pathName = this.getExternalFilesDir(null).getPath();

	File isPath = new File(pathName);
	if (!isPath.isDirectory())
		isPath.mkdirs();
	File fullName = new File(pathName + File.separator + Constants.FILE_CURRENT_GAME);

	try {
		FileOutputStream fos = new FileOutputStream(fullName);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(arScore);
		oos.close();
		fos.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public void loadCurrentGame() {
	String pathName = this.getExternalFilesDir(null).getPath();
	File fullName = new File(pathName + File.separator + Constants.FILE_CURRENT_GAME);

	try {
		FileInputStream fis = new FileInputStream(fullName);
		ObjectInputStream ois = new ObjectInputStream(fis);
		ArrayList readedObject = (ArrayList) ois.readObject();
		arScore = readedObject;
	} catch (Exception e) {
		e.printStackTrace();
	}
}


'Android' 카테고리의 다른 글

이미지 캡쳐 후 메신저로 전송  (0) 2015.03.17
Device별 DPI & 리소스 호출 순서  (0) 2015.02.13
상태바에 알림 생성  (0) 2015.01.19
TextView에 Marquee 효과 넣기  (0) 2015.01.07
ImageView의 이미지 교체  (0) 2015.01.07