1. java中的类,运行在 jvm 虚拟器中,使用方便
2. 创建 Student 类,实现 Serializable,Student.java
//transient 注释字段不需要序列化
// 添加版本号 保证添加字段时,从文件读取二进制数据转换时获得数据
public class Student implements Serializable {
private static final long serialVersionUID = 66558751374234785L;
private String name;
private int age;
private Score score;
private int foo;
public Student(String name, int age, Score score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Score getScore() {
return score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
class Score implements Serializable {
private static final long serialVersionUID = -7355990036670061294L;
private int math;
private int english;
private int chinese;
private String grade;
public Score(int math, int english, int chinese) {
this.math = math;
this.english = english;
this.chinese = chinese;
if (math >= 90 && english >= 90 && chinese >= 90) {
this.grade = "A";
} else if (math >= 80 && english >= 80 && chinese >= 80) {
this.grade = "B";
} else {
this.grade = "C";
}
}
public int getMath() {
return math;
}
public int getEnglish() {
return english;
}
public int getChinese() {
return chinese;
}
public String getGrade() {
return grade;
}
@Override
public String toString() {
return "Score{" +
"math=" + math +
", english=" + english +
", chinese=" + chinese +
", grade='" + grade + '\'' +
'}';
}
}
3. 测试调用
3.1 mian_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.05" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.45" />
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/et_age"
app:layout_constraintEnd_toEndOf="@+id/et_age"
app:layout_constraintStart_toStartOf="@+id/et_age"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Age"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/et_math"
app:layout_constraintEnd_toEndOf="@+id/et_math"
app:layout_constraintStart_toStartOf="@+id/et_math"
app:layout_constraintTop_toBottomOf="@+id/et_name" />
<EditText
android:id="@+id/et_math"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Math"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/et_english"
app:layout_constraintEnd_toEndOf="@+id/et_english"
app:layout_constraintStart_toStartOf="@+id/et_english"
app:layout_constraintTop_toBottomOf="@+id/et_age" />
<EditText
android:id="@+id/et_english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="English"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/et_chinese"
app:layout_constraintEnd_toEndOf="@+id/et_chinese"
app:layout_constraintStart_toStartOf="@+id/et_chinese"
app:layout_constraintTop_toBottomOf="@+id/et_math" />
<EditText
android:id="@+id/et_chinese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ems="10"
android:hint="Chinese"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toBottomOf="@+id/et_english" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
app:layout_constraintBottom_toTopOf="@+id/btn_load"
app:layout_constraintEnd_toEndOf="@+id/btn_load"
app:layout_constraintStart_toStartOf="@+id/btn_load"
app:layout_constraintTop_toTopOf="@+id/et_english" />
<Button
android:id="@+id/btn_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load"
app:layout_constraintBottom_toBottomOf="@+id/et_chinese"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/et_chinese"
app:layout_constraintTop_toBottomOf="@+id/btn_save" />
<TextView
android:id="@+id/tv_grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="80sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/btn_save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/et_age"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
3.2 MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText etName, etAge, etMath, etEnglish, etChinese;
private Button btSave, btLoad;
private TextView tvGrade;
private static final String FILE_NAME = "MyFile.data";
private String TAG = "MyTag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.et_name);
etAge = findViewById(R.id.et_age);
etMath = findViewById(R.id.et_math);
etEnglish = findViewById(R.id.et_english);
etChinese = findViewById(R.id.et_chinese);
btSave = findViewById(R.id.btn_save);
btLoad = findViewById(R.id.btn_load);
tvGrade = findViewById(R.id.tv_grade);
btSave.setOnClickListener(view -> {
int math = Integer.parseInt(etMath.getText().toString().trim());
int english = Integer.parseInt(etEnglish.getText().toString().trim());
int chinese = Integer.parseInt(etChinese.getText().toString().trim());
Score score = new Score(math, english, chinese);
String name = etName.getText().toString().trim();
int age = Integer.parseInt(etAge.getText().toString().trim());
Student student = new Student(name, age, score);
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(openFileOutput(FILE_NAME, MODE_PRIVATE));
objectOutputStream.writeObject(student);
objectOutputStream.flush();
objectOutputStream.close();
Toast.makeText(getApplicationContext(), "Sava data!", Toast.LENGTH_SHORT).show();
etName.getText().clear();
etAge.getText().clear();
etMath.getText().clear();
etEnglish.getText().clear();
etChinese.getText().clear();
tvGrade.setText("-");
} catch (IOException e) {
Log.e(TAG, "onClick Save", e);
}
});
btLoad.setOnClickListener(view -> {
try {
ObjectInputStream objectInputStream = new ObjectInputStream(openFileInput(FILE_NAME));
Student student = (Student) objectInputStream.readObject();
etName.setText(student.getName());
etAge.setText(String.valueOf(student.getAge()));
etMath.setText(String.valueOf(student.getScore().getMath()));
etChinese.setText(String.valueOf(student.getScore().getEnglish()));
etEnglish.setText(String.valueOf(student.getScore().getChinese()));
tvGrade.setText(student.getScore().getGrade());
Log.i(TAG, student.toString());
Toast.makeText(getApplicationContext(), "Load data!", Toast.LENGTH_SHORT).show();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "onClick Load", e);
}
});
}
}
4. 效果图