问题描述:由于序列化的对象,不能修改属性,序列化一旦保存到文件中就不可修改,并且也不能删除文件中序列化的对象。(这里可以将对象填入到集合或数组中,反序列化到内存,通过对集合的删除操作后,并将其写回文件,可以实现对象的删除)但依然无法真正意义上的删除。因此梦阳辰此次系统的书写,对于删除功能,和修改功能其实是没有实现的,只实现了,增加,查找,和遍历。注意这里没有采用new File的形式,需要自己创建文件。
对于反序列化输出多个对象存在的问题也许这篇文章能帮到你:
Java序列化及反序列化将多个对象追加到文件并读取多个对象(ObjectOutputStream,ObjectInputStream)
- 用户类:
//说明:注释代码为new了多次ObjectOutputStream的解决方案。
public class Teachers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ObjectOutputStream os = null;
FileOutputStream fileOut=null;
try {
fileOut=new FileOutputStream("src/Day32/StudentsInformation",true);
os =new ObjectOutputStream(fileOut);
} catch (IOException e) {
e.printStackTrace();
}
while(true){
System.out.println("========请选择你要进行的操作编号==========");
System.out.println("\t\t\t添加学生信息(1)\n\t\t\t浏览所有学生信息(2)");
System.out.println("\t\t\t查找学生信息(3)\n\t\t\t删除学生信息(4)");
System.out.println("\t\t\t修改学生信息(5)\n\t\t\t退出系统(-1)");
System.out.println("==========================================\n请选择功能:");
int flag = sc.nextInt();
switch (flag){
case 1:AddInformation(sc,os);break;
case 2:
try {
ViewInformation();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
case 3:
try {
FindInformation(sc);
} catch (MyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
case 4:
try {
DeleteInformation(sc);
} catch (MyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
case 5:
try {
MoidfyInformation(sc);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
case -1:sc.close();
try {
os.close();//退出系统时关闭os流
} catch (IOException e) {
e.printStackTrace();
}
return;
default:
System.out.println("The number you entered is wrong,please enter again!");
}
}
}
//Modify
private static void MoidfyInformation(Scanner sc) throws IOException, ClassNotFoundException {
ObjectInputStream ois =null;
FileInputStream out =new FileInputStream("src/Day32/StudentsInformation") ;
try {
//反序列化
ois = new ObjectInputStream(out);
} catch (IOException e) {
e.printStackTrace();
}
Students p=null;
System.out.println("请输入你要修改的学生学号:");
int num = sc.nextInt();
while(out.available()>0){//代表文件还有内容
p=(Students)ois.readObject();
if (p.getNum() == num) {
System.out.println("请输入新的名字:");
String name =sc.next();
p.setName(name);
System.out.println("请输入新的学号:");
int num1 = sc.nextInt();
p.setNum(num1);
System.out.println("请输入性别:");
String gender = sc.next();
p.setGender(gender);
System.out.println("请输入生日:(如:1999-9-10)");
String birthday =sc.next();
p.setBirthday(birthday);
System.out.println("请输入联系方式:");
String contactInformation =sc.next();
p.setContactInformation(contactInformation);
System.out.println("修改成功!");
}
}
if(ois !=null){
ois.close();
}
}
//Delete
private static void DeleteInformation(Scanner sc) throws MyException, IOException, ClassNotFoundException {
ObjectInputStream ois =null;
FileInputStream out =new FileInputStream("src/Day32/StudentsInformation") ;
try {
//反序列化
ois = new ObjectInputStream(out);
} catch (IOException e) {
e.printStackTrace();
}
Students p=null;
System.out.println("请输入你要删除的学生学号:");
int num = sc.nextInt();
while(out.available()>0){//代表文件还有内容
p=(Students)ois.readObject();
if (p.getNum() == num) {
p.setName(null);
p.setNum(0);
p.setGender(null);
p.setBirthday(null);
p.setContactInformation(null);
System.out.println("删除成功!");
}
}
if(ois!=null){
ois.close();
}
}
//Find
private static void FindInformation(Scanner sc,int...args) throws MyException, IOException, ClassNotFoundException {//可变长度参数
ObjectInputStream ois = null;
FileInputStream out = new FileInputStream("src/Day32/StudentsInformation");
try {
//反序列化
ois = new ObjectInputStream(out);
} catch (IOException e) {
e.printStackTrace();
}
Students p = null;
System.out.println("请输入你要查找的学生学号:");
int num = sc.nextInt();
while (out.available() > 0) {//代表文件还有内容
p = (Students) ois.readObject();
if (num == p.getNum()) {
System.out.println(p);
}
}
ois.close();
}
//ViewAllInformation
private static void ViewInformation() throws IOException, ClassNotFoundException {
ObjectInputStream ois =null;
FileInputStream out =new FileInputStream("src/Day32/StudentsInformation") ;
try {
//反序列化
ois = new ObjectInputStream(out);
} catch (IOException e) {
e.printStackTrace();
}
Students p=null;
/* p=(Students)ois.readObject();//从流中读取对象,需要进行强转
System.out.println(p);*/
while(out.available()>0){//代表文件还有内容
/*byte[] buf =new byte[4];
out.read(buf);//取走头部,不做处理*/
p=(Students)ois.readObject(); //读取第二个对象
System.out.println(p);
}
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//Add
private static void AddInformation(Scanner sc,ObjectOutputStream os) {
/* ObjectOutputStream os = null;
FileOutputStream fileOut=null;
try {
fileOut=new FileOutputStream("src/Day32/StudentsInformation",true);
//序列化,只能实例化一个对象流,不然添加的时候就保存到不同的地方了
os =new ObjectOutputStream(fileOut);
} catch (IOException e) {
e.printStackTrace();
}*/
System.out.println("请输入名字:");
String name =sc.next();
System.out.println("请输入学号:");
int num = sc.nextInt();
//如果要添加的学号学号存在,抛异常,提醒用户
try {
IsExist(num);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
return;//出现异常,退出方法
}
System.out.println("请输入性别:");
String gender = sc.next();
System.out.println("请输入生日:(如:1999-9-10)");
String birthday =sc.next();
System.out.println("请输入联系方式:");
String contactInformation =sc.next();
Students a =new Students(name,num,gender,birthday,contactInformation);
try {
os.writeObject(a);
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
/*finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
}
//判断是否已经存在该学号
private static void IsExist(int num) throws IOException, ClassNotFoundException, MyException {
ObjectInputStream ois = null;
FileInputStream out = new FileInputStream("src/Day32/StudentsInformation");
try {
//反序列化
ois = new ObjectInputStream(out);
} catch (IOException e) {
e.printStackTrace();
}
Students p = null;
while (out.available() > 0) {//代表文件还有内容
p = (Students) ois.readObject();
if (num == p.getNum()) {
throw new MyException("学号已存在!");//注意抛了异常就不会执行下面代码了,所以不用return
}
}
ois.close();
}
}
- 学生类:
public class Students implements Serializable {
private static final long serizlVersionUID=646646465464646546L;
private String name;
private int num;//学号
private String gender;//性别
private String birthday;//生日
private String ContactInformation;//联系方法
//构造方法
public Students() {
}
public Students(String name, int num, String gender, String birthday, String contactInformation) {
this.name = name;
this.num = num;
this.gender = gender;
this.birthday = birthday;
ContactInformation = contactInformation;
}
public static long getSerizlVersionUID() {
return serizlVersionUID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getContactInformation() {
return ContactInformation;
}
public void setContactInformation(String contactInformation) {
ContactInformation = contactInformation;
}
@Override
public String toString() {
return "Students{" +
"name='" + name + '\'' +
", num=" + num +
", gender='" + gender + '\'' +
", birthday=" + birthday +
", ContactInformation='" + ContactInformation + '\'' +
'}';
}
}
- 自定义异常类:
public class MyException extends Exception {
public MyException() {
}
public MyException(String message) {
super(message);
}
}