一、注意事项
在使用对象流存取对象时,需要保证对象是序列化的,因为文件是字节流。
二、ObjectOutPutStream
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(openFileOutput(FILE_NAME,MODE_PRIVATE));
objectOutputStream.writeObject(student);
objectOutputStream.flush();
objectOutputStream.close();
Toast.makeText(MainActivity.this, "Data Saved!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e(TAG,"onClick:",e);
}
三、ObjectInputStream
try {
ObjectInputStream objectInputStream = new ObjectInputStream(openFileInput(FILE_NAME));
Student student =(Student) objectInputStream.readObject();
textView.setText(student.getScore().getGrade().toString());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}