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.io.Serializable;
public class ObjectDemo
{
public static void main(String[] args) {
testObjectOutputStream();
testObjectInputStream();
}
//对象的反序列化,将硬盘中的文件提供ObjectInputStream转换为需要的对象;
public static void testObjectInputStream()
{
ObjectInputStream ois = null;
try{
ois = new ObjectInputStream(new FileInputStream("d:\\a\\a.txt"));
Person p1 = (Person)ois.readObject();
System.out.println(p1);
Person p2 = (Person)ois.readObject();
System.out.println(p2);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if(ois != null)
{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中;
public static void testObjectOutputStream()
{
Person p1 = new Person("xiao", 23);
Person p2 = new Person("da", 21);
ObjectOutputStream oos = null;
try
{
oos = new ObjectOutputStream(new FileOutputStream(new File("d:\\a\\a.txt")));
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if(oos != null)
{
try
{
oos.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
class Person implements Serializable //实现Serializable接口
{
//实现Serializable接口的类都有一个UID;
private static final long serialVersionUID = 24367;
String name;
Integer age;
public Person(String name, Integer age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
return "name" + this.name + "age" +this.age;
}
}
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.io.Serializable;
public class ObjectDemo
{
public static void main(String[] args) {
testObjectOutputStream();
testObjectInputStream();
}
//对象的反序列化,将硬盘中的文件提供ObjectInputStream转换为需要的对象;
public static void testObjectInputStream()
{
ObjectInputStream ois = null;
try{
ois = new ObjectInputStream(new FileInputStream("d:\\a\\a.txt"));
Person p1 = (Person)ois.readObject();
System.out.println(p1);
Person p2 = (Person)ois.readObject();
System.out.println(p2);
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if(ois != null)
{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//对象的序列化过程,将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘文件中;
public static void testObjectOutputStream()
{
Person p1 = new Person("xiao", 23);
Person p2 = new Person("da", 21);
ObjectOutputStream oos = null;
try
{
oos = new ObjectOutputStream(new FileOutputStream(new File("d:\\a\\a.txt")));
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if(oos != null)
{
try
{
oos.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
}
class Person implements Serializable //实现Serializable接口
{
//实现Serializable接口的类都有一个UID;
private static final long serialVersionUID = 24367;
String name;
Integer age;
public Person(String name, Integer age)
{
this.name = name;
this.age = age;
}
@Override
public String toString()
{
return "name" + this.name + "age" +this.age;
}
}