具体的代码实现和技巧确实是从哪个不知名的站点找到的,内容是自己测试排版弄出来的,推广知识而已
对象的序列化是指对象实现接口java.io.Serializable
第一个示例
package com.serializable.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student1 implements Serializable {
private static final long serialVersionUID = -2193347404055790145L;
public int studentid;
public static String studentName;
}
/**
* @author Administrator
*
*正常使用对象的系列化,对象用于对象流在文件中的输入输出
*/
public class ObjectIO1{
/**
* @param args
*/
public static void main(String[] args) {
ObjectIO1 io = new ObjectIO1();
try {
io.write();
io.read();
} catch (Exception e) {
e.printStackTrace();
}
}
public void write() throws Exception {
Student1 s = new Student1();
s.studentid = 10;
s.studentName = "JJOY";
FileOutputStream fileOutputStream = new FileOutputStream("xxxx");
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
oos.writeObject(s);
oos.close();
}
public void read() throws Exception {
FileInputStream fileInputStream = new FileInputStream("xxxx");
ObjectInputStream ois = new ObjectInputStream(fileInputStream);
Object o = ois.readObject();
if (o instanceof Student1) {
Student1 s = (Student1) o;
System.out.println(s.studentid);//返回10
System.out.println(s.studentName);//返回JJOY
}
ois.close();
}
}
输出结果是10
JJOY
第二个示例
package com.serializable.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* @author Administrator
*
*对象的序列化时类的静态属性不和某个具体的的对象绑定在一起,
*后来通过类直接对属性重新复制,取出来得值是后面赋的新值
*
*/
public class ObjectIO2 {
/**
* @param args
*/
public static void main(String[] args) {
ObjectIO2 io = new ObjectIO2();
try {
io.write();
io.read();
} catch (Exception e) {
e.printStackTrace();
}
}
public void write() throws Exception {
Student2 s = new Student2();
s.studentid = 10;
s.studentName = "中文名";//某个具体对象属性的赋值,但这是类的静态属性
FileOutputStream fileOutputStream = new FileOutputStream("xxxx");
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
oos.writeObject(s);
oos.close();
}
public void read() throws Exception {
FileInputStream fileInputStream = new FileInputStream("xxxx");
ObjectInputStream ois = new ObjectInputStream(fileInputStream);
Object o = ois.readObject();
if (o instanceof Student2) {
Student2 s = (Student2) o;
Student2.studentName = "王小二";//后来通过类直接对属性重新复制
System.out.println(s.studentid);
System.out.println(s.studentName);//取出来得值是后面赋的新值
}
ois.close();
}
}
class Student2 implements Serializable {
private static final long serialVersionUID = -2193347404055790145L;
public int studentid;
public static String studentName;
}
运行结果
10
王小二
示例3
package com.serializable.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author Administrator
* 理解transient关键字的作用,对类的属性加上transient关键字, transient是临时保持值(瞬间值),
* 对象序列化时不能对其中的属性进行序列化,对其中的属性重新赋值也是无效的,这样有利于数据在网路上的传输,
* 尤其是设计账号和密码之类的安全性问题,即使在初始化对studentid赋值为5之类的,最终输出仍然是0
*
*/
class Student3 implements Serializable {
private static final long serialVersionUID = -2193347404055790145L;
public transient int studentid=5;// 在对象序列化时不能对studentid属性进行序列化
public static String studentName;
}
public class ObjectIO3 {
/**
* @param args
*/
public static void main(String[] args) {
ObjectIO3 io = new ObjectIO3();
try {
io.write();
io.read();
} catch (Exception e) {
e.printStackTrace();
}
}
public void write() throws Exception {
Student3 s = new Student3();
s.studentid = 10;//对其中的属性重新赋值也是无效的
s.studentName = "中文名";
FileOutputStream fileOutputStream = new FileOutputStream("xxxx");
ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
oos.writeObject(s);
oos.close();
}
public void read() throws Exception {
FileInputStream fileInputStream = new FileInputStream("xxxx");
ObjectInputStream ois = new ObjectInputStream(fileInputStream);
Object o = ois.readObject();
if (o instanceof Student3) {
Student3 s = (Student3) o;
Student3.studentName = "王小二";
System.out.println(s.studentid);//这里的运行结果是0
System.out.println(s.studentName);//王小二
}
ois.close();
}
}
运行结果
0
王小二
示例4
package com.serializable.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class People {//这个类没有实现Serializable接口
public int age;
public People() {
System.out.println("People1");
}
}
class Student extends People implements Serializable {
public transient int studentId = 5;
public static String studentName;
public int marks ;//学生分数
public Student() {
System.out.println("Student2");
}
}
/**
* Peope类没有实现Serializable接口,故向文件写入对象时,没有完成people对象的序列化工作,再从文件中读取对象,需要对对象
* 的属性重新初始化工作,故后来打印再次打印people,打印出学生的年龄是0。
* @author Administrator
*
*/
public class ObjectIO4 {
public void write() throws Exception {
Student s = new Student();
s.studentId = 10;
s.age = 20;
s.studentName = "名字name";
s.marks =80;
FileOutputStream fos = new FileOutputStream("XXXX");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
oos.close();
}
public void read() throws Exception {
FileInputStream fis = new FileInputStream("XXXX");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
Student.studentName = "王小二";
if (o instanceof Student) {
Student s = (Student) o;
System.out.println("学生的学号:" + s.studentId);//0
System.out.println("学生的姓名:" + s.studentName);//王小二
System.out.println("学生的成绩:" + s.marks);//80
System.out.println("学生的年龄:" + s.age);//0
}
ois.close();
}
public static void main(String[] args) {
try {
ObjectIO4 oo = new ObjectIO4();
oo.write();
oo.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意这次的输出结果
People1
Student2
People1
学生的学号:0
学生的姓名:王小二
学生的成绩:80
学生的年龄:0
示例5
package com.serializable.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* Peope2类没有实现Serializable接口,向文件写入对象时,
* 没有完成该类对象的序列化工作,在从文件中读取对象,需要对对象的
* 属性重新初始化工作,故后来打印再次打印People,打印出学生的年龄是25。
* @author Administrator
*
*/
class People2 {
public int age;
public People2() {
age = 25;
System.out.println("People");
}
}
class Student4 extends People2 implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public transient int studentId = 5;
public static String studentName;
public Student4() {
System.out.println("Student");
}
}
public class ObjectIO5 {
public void write() throws Exception {
Student4 s = new Student4();
s.studentId = 10;
s.age = 20;
s.studentName = "justName";
FileOutputStream fos = new FileOutputStream("XXXX");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
oos.close();
}
public void read() throws Exception {
FileInputStream fis = new FileInputStream("XXXX");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
Student4.studentName = "王二小";
if (o instanceof Student4) {
Student4 s = (Student4) o;
System.out.println("学生的学号:" + s.studentId);//0
System.out.println("学生的姓名:" + s.studentName);
System.out.println("学生的年龄:" + s.age);//25
}
ois.close();
}
public static void main(String[] args) {
try {
ObjectIO5 oo = new ObjectIO5();
oo.write();
oo.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果
People
Student
People -- 再次初始化,构造方法里打印的字符串
学生的学号:0
学生的姓名:王二小
学生的年龄:25--使用构造方法里定义的属性初始化对象,然后打印25
示例6
package com.serializable.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* Peope3类实现Serializable接口向文件写入对象时,
* 完成people3对象的序列化工作,在从文件中读取对象,无需要对对象的属性重新
* 初始化工作,打印出学生的年龄是20。即使在People类添加age=50这样的语句,
* 最终输出年龄的值任然是20,因为后来没有再次初始化People类
* @author Administrator
*
*/
class People3 implements Serializable {
public int age=50;//在People类添加age=50的赋值,但是在将对象写入文件前,修改了一次属性
public People3() {
System.out.println("People");
}
}
class Student5 extends People3 {
public transient int studentId = 5;
public static String studentName;
public Student5() {
System.out.println("Student");
}
}
public class ObjectIO6 {
public void write() throws Exception {
Student5 s = new Student5();
s.studentId = 10;
s.age = 20;//对象写入文件前先修改了值
s.studentName = "xxxx";
FileOutputStream fos = new FileOutputStream("XXXX");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
oos.close();
}
public void read() throws Exception {
FileInputStream fis = new FileInputStream("XXXX");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
Student5.studentName = "王二小";
if (o instanceof Student5) {
Student5 s = (Student5) o;
System.out.println("学生的学号:" + s.studentId);
System.out.println("学生的姓名:" + s.studentName);
System.out.println("学生的年龄:" + s.age);//20,假如再次初始化对象,则输出为50
}
ois.close();
}
public static void main(String[] args) {
try {
ObjectIO6 oo = new ObjectIO6();
oo.write();
oo.read();
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出结果
People
Student---这里没有再打印People 说明没有再次初始化
学生的学号:0
学生的姓名:王二小
学生的年龄:20