1.设置序列化的对象实体
package demo02;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
2.test.java文件实现对学生的具体操作
package demo02;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
while(true){
System.out.println("=====================欢迎光临千峰学生管理系统====================");
System.out.println("1:添加学生信息!");
System.out.println("2:删除学生信息!");
System.out.println("3:查看学生信息!");
System.out.println("4:修改学生信息!");
System.out.println("5:退出登录!");
Scanner sc=new Scanner(System.in);
System.out.println("请选择您的操作:");
int choose=sc.nextInt();
switch (choose) {
case 1:
addStudent();
break;
case 2:
delStudent();
break;
case 3:
findStudent();
break;
case 4:
modifyStudent();
break;
case 5:
System.out.println("退出成功!");
break;
}
System.out.println("请按任意键回到首页");
int id=sc.nextInt();
}
}
private static void modifyStudent() {
File file=new File("F:\\学习资料\\student.txt");
Scanner sc=new Scanner(System.in);
System.out.println("请输入修改学生的学号");
int id=sc.nextInt();
System.out.println("请输入修改学生的姓名");
String name=sc.next();
System.out.println("请输入修改学生的年龄");
int age=sc.nextInt();
Student stu=new Student(id,name,age);
List<Student>list=new ArrayList<Student>();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
list=(List<Student>) ois.readObject();
List<Student>list2=new ArrayList<Student>();
for (Student student : list) {
if(id!=student.getId()){
list2.add(stu);
}
}
list2.add(stu);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list2);
System.out.println("修改成功!");
oos.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void findStudent() {
File file=new File("F:\\学习资料\\student.txt");
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
List<Student>list=new ArrayList<Student>();
list=(List<Student>) ois.readObject();
for (Student student : list) {
System.out.println(student.toString());
}
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void delStudent() {
File file=new File("F:\\学习资料\\student.txt");
Scanner sc=new Scanner(System.in);
System.out.println("请输入删除学生的学号");
int id=sc.nextInt();
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
List<Student>list=new ArrayList<Student>();
List<Student>list2=new ArrayList<Student>();
list=(List<Student>) ois.readObject();
for (Student student : list) {
if(id!=student.getId()){
list2.add(student);
}
}
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list2);
System.out.println("删除成功!");
oos.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void addStudent() {
File file=new File("F:\\学习资料\\student.txt");
Scanner sc=new Scanner(System.in);
System.out.println("请输入添加学生的学号");
int id=sc.nextInt();
System.out.println("请输入添加学生的姓名");
String name=sc.next();
System.out.println("请输入添加学生的年龄");
int age=sc.nextInt();
Student stu=new Student(id,name,age);
List<Student>list=new ArrayList<Student>();
if(file.exists()){
try {
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
list=(List<Student>)ois.readObject();
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
list.add(stu);
oos.writeObject(list);
System.out.println("学生添加成功!");
oos.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
list.add(stu);
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(list);//添加学生对象
System.out.println("添加成功!");
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}