Android

ArrayList 파일로 저장 및 읽기

moonwoou 2015. 3. 17. 11:49
경로 예
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();
	}
}