从键盘上获取名字、语文、数学的成绩,计算总分然后填充到Student对象中,并将对象写入文件(student.dat)。
class Student{
String name;
double Chinese;
double math;
double total;
//计算总分方法
}
将对象序列化存入文件,反序列显示出来!
class Student{
String name;
double Chinese;
double math;
double total;
//计算总分方法
}
将对象序列化存入文件,反序列显示出来!
贴一段我以前写的保存数据的方法,供LZ参考:
public void saveData(List<Employee> list, boolean hasHead)
throws BusinessException {
BufferedWriter bw = null;
Employee p = new Employee(null, 0, null);
try {
if (hasHead) {
bw = new BufferedWriter(new FileWriter("D:\\Exployee.dat"));
bw.write("总共有" + list.size() + "个雇员的记录. \n");
bw.write(" 姓名 薪水 入职日期 \n");
for (int i = 0; i < list.size(); i++) {
p = list.get(i);
bw.write((i + 1) + "." + p.getName() + "\t" + p.getSalary()
+ "\t" + p.getEnter_date() + "\n");
}
} else {
bw = new BufferedWriter(new FileWriter("D:\\exployee_bak.dat"));
for (int i = 2; i < list.size(); i++) {
p = list.get(i);
bw.write((i + 1) + "." + p.getName() + "\t" + p.getSalary()
+ "\t" + p.getEnter_date() + "\n");
}
} } catch (Exception e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}