下面是我的程序,有个小小的错误 我找不到啊 请高手指点。import java.io.*;
public class ObjectSerialization
{
public static void main(String[] args) throws IOException,ClassNotFoundException
{
Student st = new Student(20010901,"黎明",20,"计算机系");
FileOutputStream fout = new FileOutputStream("data.txt");
ObjectOutputStream sout = new ObjectOutputStream(fout);
try
{
sout.writeObject(st);
sout.close();
}
catch(IOException e)
{
}
st = null;
FileInputStream fin = new FileInputStream("data.txt");
ObjectInputStream sin = new ObjectInputStream(fin);
try
{
st = (Student)sin.readObject();
sin.close();
}
catch(IOException e)
{
}
System.out.println("学生信息:");
System.out.println("学号:"+st.id);
System.out.println("姓名:"+st.name);
System.out.println("年龄:"+st.age);
System.out.println("院系:"+st.department);
}
class Student implements Serializable
{
static int total;
transient boolean isEnrolled;
int id;
String name;
int age;
String department;
public Student()
{
}
public Student(int id,String name,int age,String department)
{
this.id = id;
this.name = name;
this.age = age;
this.department = department;
total++;
isEnrolled = true;
}
private void writeObject(ObjectOutputStream out) throws IOException
{
out.writeInt(id);
out.writeInt(age);
out.writeUTF(name);
out.writeUTF(department);
}
private void readObject(ObjectInputStream in) throws IOException
{
id = in.readInt();
age = in.readInt();
name = in.readUTF();
department = in.readUTF();
}
}
}
public class ObjectSerialization
{
public static void main(String[] args) throws IOException,ClassNotFoundException
{
Student st = new Student(20010901,"黎明",20,"计算机系");
FileOutputStream fout = new FileOutputStream("data.txt");
ObjectOutputStream sout = new ObjectOutputStream(fout);
try
{
sout.writeObject(st);
sout.close();
}
catch(IOException e)
{
}
st = null;
FileInputStream fin = new FileInputStream("data.txt");
ObjectInputStream sin = new ObjectInputStream(fin);
try
{
st = (Student)sin.readObject();
sin.close();
}
catch(IOException e)
{
}
System.out.println("学生信息:");
System.out.println("学号:"+st.id);
System.out.println("姓名:"+st.name);
System.out.println("年龄:"+st.age);
System.out.println("院系:"+st.department);
}
class Student implements Serializable
{
static int total;
transient boolean isEnrolled;
int id;
String name;
int age;
String department;
public Student()
{
}
public Student(int id,String name,int age,String department)
{
this.id = id;
this.name = name;
this.age = age;
this.department = department;
total++;
isEnrolled = true;
}
private void writeObject(ObjectOutputStream out) throws IOException
{
out.writeInt(id);
out.writeInt(age);
out.writeUTF(name);
out.writeUTF(department);
}
private void readObject(ObjectInputStream in) throws IOException
{
id = in.readInt();
age = in.readInt();
name = in.readUTF();
department = in.readUTF();
}
}
}
这样就OKle