import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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;
public class ObjectSerializeDemo {
public static void main(String[] args) {
String path="D:/aa.txt";
serialeTo(path);
seriale(path);
}
//反序列化
public static void seriale(String srcpath){
try(
ObjectInputStream bis=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File(srcpath))));
){
Object obj=bis.readObject();
Student st=null;
if(obj instanceof Student){
st=(Student)obj;
}
//bis.close();
System.out.println(st.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//序列化
public static void serialeTo(String destpath){
//创建数据源
Student st=new Student("杨幂",33);
File fos=new File(destpath);//无论保存什么格式类型的文件,都是乱码,这里
try(//jdk1.7,资源关闭新特性
ObjectOutputStream dos=new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fos)));
){
dos.writeObject(st);
dos.flush();//刷新管道数据
//dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//要实现对象反序列化/序列化必须实现Serializable接口
class Student implements java.io.Serializable{
private String name;
private transient int age;//不需要序列化,就用transient修饰
public Student() {
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
import java.io.BufferedOutputStream;
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;
public class ObjectSerializeDemo {
public static void main(String[] args) {
String path="D:/aa.txt";
serialeTo(path);
seriale(path);
}
//反序列化
public static void seriale(String srcpath){
try(
ObjectInputStream bis=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(
new File(srcpath))));
){
Object obj=bis.readObject();
Student st=null;
if(obj instanceof Student){
st=(Student)obj;
}
//bis.close();
System.out.println(st.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//序列化
public static void serialeTo(String destpath){
//创建数据源
Student st=new Student("杨幂",33);
File fos=new File(destpath);//无论保存什么格式类型的文件,都是乱码,这里
try(//jdk1.7,资源关闭新特性
ObjectOutputStream dos=new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(fos)));
){
dos.writeObject(st);
dos.flush();//刷新管道数据
//dos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//要实现对象反序列化/序列化必须实现Serializable接口
class Student implements java.io.Serializable{
private String name;
private transient int age;//不需要序列化,就用transient修饰
public Student() {
}
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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
本文介绍了一个Java程序示例,演示了如何将一个实现了Serializable接口的Student对象进行序列化和反序列化操作。通过使用ObjectOutputStream和ObjectInputStream类,该示例展示了如何将对象状态保存到文件中以及从文件中读取对象状态。
1362

被折叠的 条评论
为什么被折叠?



