public class StuOpration {
static File file = new File("student.txt");
public void writeStreamheader(){}
//添加一个学生
public static void get() throws FileNotFoundException{
FileOutputStream os = new FileOutputStream(file,true);
}
public static void add(Student stu) throws Exception{
if(file.length() ==0){
get();
}
FileOutputStream os = new FileOutputStream(file,true);
ObjectOutputStream oos = new ObjectOutputStream(os );
oos.writeObject(stu);
oos.writeChars("\n");
oos.close();
System.out.println("添加成功");
}
//修改学生信心
public void update(int num,int age) throws Exception{
FileInputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream( is);
Student stu = (Student)ois.readObject();
while(is.available()>0){
if(stu.getNum() == num){
stu.setAge(age);
System.out.println("修改成功");
return;
}
stu = (Student)ois.readObject();
}
ois.close();
}
//删除一个学生
public void delete(int num) throws Exception{
FileInputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream( is);
Student stu = (Student)ois.readObject();
while(is.available()>0){
System.out.println(stu);
stu = (Student)ois.readObject();
}
ois.close();
}
public void show() throws Exception{
FileInputStream is = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream( is);
Student obj=(Student)ois.readObject();
while(true){
try {
System.out.println(obj);
obj=(Student)ois.readObject();
} catch (Exception e) {
System.out.println("数据已经读取完毕");
break;
}
}
ois.close();
}
}
请输入对应的编号:
1: 添加学生 2: 修改学生信息 3:删除学生4:显示学生信息
4
Student [name=张三, password=123, score=45.0, num=1, age=23]
数据已经读取完毕