为什么序列化对象数组那里出错了?我改成单个的就可以运行,但是换成数组就会运行报错,什么原因?怎么改?
import java.io.Serializable;
public class Person implements Serializable{
private String name ;
private int age ;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return name + " " + age;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class FileOperator {
public static void ser(Person per) throws Exception {
File file = new File("D:\\ps.txt") ;
OutputStream out = new FileOutputStream(file , true) ;
ObjectOutputStream ob1 = new ObjectOutputStream( out ) ;
ob1.writeObject(per);
ob1.writeObject(null) ;
System.out.println("已增加数据");
ob1.close();
out.close();
}
public static void delete(Person per) throws Exception{
File file = new File("D:\\ps.txt") ;
InputStream in2 = new FileInputStream( file ) ;
OutputStream out2 = new FileOutputStream( file,false ) ;
ObjectInputStream ob2 = new ObjectInputStream( in2 ) ;
Person p[] = (Person[])ob2.readObject() ;
ob2.close();
in2.close();
for(int i=0; i<p.length; i++ ){
if(p[i].getName().equals(per.getName())){
String s = null ;
byte by[] = s.getBytes() ;
out2.write( by );
System.out.println("已删除数据");
out2.close();
}
}
}
public static void look(Person p) throws Exception {
File file = new File("D:\\ps.txt") ;
InputStream in3 = new FileInputStream( file ) ;
ObjectInputStream ob3 = new ObjectInputStream( in3 ) ;
Person pt[] = (Person[])ob3.readObject() ;
ob3.close();
in3.close();
System.out.println("已找到数据");
for(int j=0; j<pt.length; j++){
if(pt[j].getName().equals(p.getName())){
System.out.println("已找到数据");
}
}
}
public static void change(Person p,Person p1) throws Exception{
File file = new File("D:\\ps.txt") ;
InputStream in4 = new FileInputStream( file ) ;
OutputStream in44 = new FileOutputStream( file ) ;
ObjectInputStream ob4 = new ObjectInputStream( in4 ) ;
ObjectOutputStream ob44 = new ObjectOutputStream( in44 ) ;
OutputStream out4 = new FileOutputStream( file,false );
Object per[] = (Object[])ob4.readObject() ;
ob4.close();
in4.close();
for(int t=0; t<per.length; t++){
if(((File) per[t]).getName().equals( p.getName() )){
ob44.writeObject( p1 );
ob44.writeObject(null);
System.out.println("已修改数据");
out4.close();
}
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in) ;
System.out.println("1: 增加数据");
System.out.println("2: 删除数据");
System.out.println("3: 修改数据");
System.out.println("4: 查找数据");
System.out.println("5: 系统退出");
System.out.println("请输入操作指数");
while(sc.hasNext()){
int n = sc.nextInt() ;
switch( n ){
case 1:
System.out.println("请输入增加的数据");
Person p1 = new Person(sc.next(),sc.nextInt());
FileOperator.ser(p1);
break ;
case 2:
System.out.println("请输入要删除的数据:");
Person p2 = new Person(sc.next(),sc.nextInt()) ;
FileOperator.delete(p2);
break ;
case 3:
System.out.println("请输入原数据及修改的数据");
Person p3 = new Person(sc.next(),sc.nextInt()) ;
Person p33 = new Person(sc.next(),sc.nextInt()) ;
FileOperator.change(p3,p33);
break ;
case 4:
System.out.println("请输入查找的数据");
Person p4 = new Person(sc.next(),sc.nextInt()) ;
FileOperator.look(p4);
break ;
case 5:
System.exit(1);
break ;
}
}
}
}
在序列化对象数组时,需要注意以下几点:
对象数组需要使用“[]”来定义,例如“Person[] persons = new Person[3]”。
对象数组中的每个元素都需要进行初始化,例如“persons[0] = new Person("Tom", 20)”等。
序列化和反序列化时,需要使用“ObjectOutputStream”和“ObjectInputStream”来操作对象数组。
下面是一个示例代码,用于序列化和反序列化对象数组:
import java.io.*;
public class Main {
public static void main(String[] args) {
Person[] persons = new Person[3];
persons[0] = new Person("Tom", 20);
persons[1] = new Person("Jerry", 25);
persons[2] = new Person("Alice", 30);
try {
// 序列化对象数组
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("persons.dat"));
oos.writeObject(persons);
oos.close();
// 反序列化对象数组
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("persons.dat"));
Person[] newPersons = (Person[]) ois.readObject();
ois.close();
// 输出反序列化后的对象数组
for (Person p : newPersons) {
System.out.println(p);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}