序列化(ObjectOutputStream和ObjectInputStream)(切记:out是输出到本地中,in是输入到程序中)
2014-07-04 23:32
429 查看
注意:序列化自定义类必须实现一个接口Serializable,在c#中序列化自定义类是使用特性也就是[Serializable]
//要实现序列化的类
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;//序列版本号
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
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;
}
}
//ObjectOutputStream
public class ObjectOutputStreamDemo {
/**
* 序列化步骤:
* 1、创建字节流
* 2、创建序列化流
* 3、序列化操作
* 4、关闭流
*/
//使用序列化前提:保证进行序列化的类要实现Serializable接口
public static void main(String[] args) {
Student stu = new Student("张三",23);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
//1、创建字节流
fos =new FileOutputStream("student.stu");
//2、创建序列化流
oos = new ObjectOutputStream(fos);
//序列化操作
oos.writeObject(stu);
System.out.println("序列化成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//4、关闭流
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//ObjectInputStream
public class ObjectInputStreamDemo {
//反序列化
public static void main(String[] args) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("student.stu");
ois = new ObjectInputStream(fis);
Student student = (Student)ois.readObject();
System.out.println(student.getName()+"-"+student.getAge());
} 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();
} finally{
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//要实现序列化的类
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;//序列版本号
private String name;
private int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
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;
}
}
//ObjectOutputStream
public class ObjectOutputStreamDemo {
/**
* 序列化步骤:
* 1、创建字节流
* 2、创建序列化流
* 3、序列化操作
* 4、关闭流
*/
//使用序列化前提:保证进行序列化的类要实现Serializable接口
public static void main(String[] args) {
Student stu = new Student("张三",23);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
//1、创建字节流
fos =new FileOutputStream("student.stu");
//2、创建序列化流
oos = new ObjectOutputStream(fos);
//序列化操作
oos.writeObject(stu);
System.out.println("序列化成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//4、关闭流
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//ObjectInputStream
public class ObjectInputStreamDemo {
//反序列化
public static void main(String[] args) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("student.stu");
ois = new ObjectInputStream(fis);
Student student = (Student)ois.readObject();
System.out.println(student.getName()+"-"+student.getAge());
} 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();
} finally{
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相关文章推荐
- FileOutputStream字节输出流和FileInputStream输入流(切记:out是输出到本地中,in是输入到程序中)这里介绍大文件和小文件的读取方式
- DataInputStream(二进制输入流)和DataOutputStream二进制输出流(注意:in是从本地文件输入到程序中,out是从程序输出到本地种)
- (JAVA)从零开始之--对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
- 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)
- 使用对象输入输出流(ObjectOutputStream,ObjectInputStream)实现序列化对象的深度克隆
- 对象序列化到本地文件 ObjectOutputstream ObjcetInputstream
- Socket同时使用ObjectInputStream和ObjectOutputStream传输序列化对象时的顺序
- Java对象序列化ObjectOutputStream和ObjectInputStream示例
- java语言编程IO流之对象序列化和ObjectInputStream与ObjectOutputStream
- java中如何执行本地程序并捕获out和err输出
- 如何执行本地程序并捕获out和err输出(written in Java)
- [导入]java中如何执行本地程序并捕获out和err输出
- 输入输出之InputStream&OutputStream、Reader&Writer
- ObjectInputStream 和 ObjectOutputStream(对象的序列化、持久化)
- System.in与System.out(标准输入与标准输出)详解
- IO 输入与输出(3) -- 节点流之一InputStream和OutputStream
- Java基础之对象的序列化(持久化)操作对象ObjectInputStream/ObjectOutputStream
- JAVA---System.in与System.out(标准输入与输出)详解
- System.in与System.out(标准输入与标准输出)详解
- 通过ObjectOutputStream序列化和使用ObjectInputStream反序列化