당신은 행운에있어 '를 멤버가 이미 serialzble 있습니다 그래서 당신의 첫 번째 단계는 강의 직렬화라고 말할 것입니다.
public class Lecture implements Serializable {
public String title;
public String startTime;
public String endTime;
public String day;
public boolean classEnabled;
public Lecture(String title, String startTime, String endTime, String day, boolean enable){
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
this.day = day;
this.classEnabled = enable;
}
다음으로 직렬화에 필요하므로 기본 생성자를 만들어야합니다. 마지막으로 객체를 파일에 써야합니다. 나는 보통 다음과 같은 것을 사용한다. 캐시 디렉토리를 사용하지 않기 위해 게임 상태를 저장하는 것입니다.
private void saveState() {
final File cache_dir = this.getCacheDir();
final File suspend_f = new File(cache_dir.getAbsoluteFile() + File.separator + SUSPEND_FILE);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
boolean keep = true;
try {
fos = new FileOutputStream(suspend_f);
oos = new ObjectOutputStream(fos);
oos.writeObject(this.gameState);
}
catch (Exception e) {
keep = false;
Log.e("MyAppName", "failed to suspend", e);
}
finally {
try {
if (oos != null) oos.close();
if (fos != null) fos.close();
if (keep == false) suspend_f.delete();
}
catch (Exception e) { /* do nothing */ }
}
}
데이터를 다시 읽는 것은 쓰기와 상당히 대칭입니다. 그래서 나는이 대답을 남겼습니다. 또한 직렬화 된 객체에 대한 많은주의 사항이 있으므로 Google 검색을 수행하고 Java 직렬화에 대한 일반적인 내용을 읽어 보시기 바랍니다.
public class RegionList extends ArrayList<Region> {} // Region implements Serializeable
내가 같은 코드를 사용하여 저장하려면
설명하면서 마이너스는, 당신의 대답을 발견 검색 좀! – l0gg3r