当使用Serializable接口实现序列化操作时,如果一个对象中的某一属性不希望被序列化,则可以使用transient关键字进行声明
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable; //=================================================
// File Name : Serializable_demo
//------------------------------------------------------------------------------
// Author : Common //类名:Person_3
//属性:
//方法:
class Person_3 implements Serializable{ //此类的对象可以被序列化
private transient String name;
private int age; public Person_3(String name, int age) {
super();
this.name = name;
this.age = age;
} @Override
public String toString() {
return "姓名:" + name + ", 年龄:" + age;
} } //主类
//Function : Serializable_demo;
public class Serializable_demo { public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
// File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
// ObjectOutputStream oos = null;
// OutputStream out = new FileOutputStream(f); //文件输出流
// oos = new ObjectOutputStream(out); //为对象输出流实例化
// oos.writeObject(new Person_3("张三", 30));
// oos.close(); File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
ObjectInputStream ois = null;
InputStream input = new FileInputStream(f); //文件输入流
ois = new ObjectInputStream(input); //为对象输入流实例化
Object obj = ois.readObject(); //读取对象
ois.close();
System.out.println(obj);
} }
<3>序列化一组对象
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable; //=================================================
// File Name : Serializable_demo
//------------------------------------------------------------------------------
// Author : Common //类名:Person_3
//属性:
//方法:
class Person_3 implements Serializable{ //此类的对象可以被序列化
// private transient String name;
private String name;
private int age; public Person_3(String name, int age) {
super();
this.name = name;
this.age = age;
} @Override
public String toString() {
return "姓名:" + name + ", 年龄:" + age;
} } //主类
//Function : Serializable_demo;
public class Serializable_demo { public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
// File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
// ObjectOutputStream oos = null;
// OutputStream out = new FileOutputStream(f); //文件输出流
// oos = new ObjectOutputStream(out); //为对象输出流实例化
// oos.writeObject(new Person_3("张三", 30));
// oos.close(); // File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
// ObjectInputStream ois = null;
// InputStream input = new FileInputStream(f); //文件输入流
// ois = new ObjectInputStream(input); //为对象输入流实例化
// Object obj = ois.readObject(); //读取对象
// ois.close();
// System.out.println(obj); Person_3 per[] = {new Person_3("张三",30),new Person_3("李四",31),new Person_3("王五",32)};//定义对象数组
ser(per); //序列化对象数组
Object o[] = dser();
for(int i=0;i<o.length;i++){
Person_3 p = (Person_3) o[i];
System.out.println(p);
}
} public static void ser(Object obj[]) throws Exception{
File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
ObjectOutputStream oos = null;
OutputStream out = new FileOutputStream(f); //文件输出流
oos = new ObjectOutputStream(out); //为对象输出流实例化
oos.writeObject(obj);
oos.close();
} public static Object[] dser() throws Exception{
File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
ObjectInputStream ois = null;
InputStream input = new FileInputStream(f); //文件输入流
ois = new ObjectInputStream(input); //为对象输入流实例化
Object obj[] = (Object[])ois.readObject(); //读取对象数组
ois.close();
return obj;
} }