/** |
* 这个类可以避免StreamCorruptedException |
* @author Liao |
* |
*/ |
public class MyObjectOutputStream extends ObjectOutputStream { |
//无参构造函数 |
public MyObjectOutputStream() throws IOException, SecurityException { |
super (); |
} |
//有参构造函数 |
public MyObjectOutputStream(OutputStream out) throws IOException { |
super (out); |
} |
|
/** |
* 重写writeStreamHeader()方法 |
*/ |
@Override |
protected void writeStreamHeader() throws IOException { |
// TODO Auto-generated method stub |
return ; |
} |
} |
//源代码片段来自云代码http://yuncode.net |
工具类: |
[java] view plaincopy |
public class SerializableUtil implements Serializable{ |
|
private static final long serialVersionUID = 1L; |
|
/** |
* 序列化对象 |
* 用户传一个Object类型对象,和要序列化到的文件路径即可进行序列化 |
* @param obj对象 |
* @param path 对象要被序列化到的文件路径 |
*/ |
public static void serializObject(Object obj,String path){ |
|
//创建文件 |
File file = new File(path); |
//判断文件是否存在 |
if (!file.exists()){ |
//如果文件不存在,就创建文件 |
try { |
file.createNewFile(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
//创建ObjectOutputStream |
ObjectOutputStream objectOutputStream = null ; |
|
try { |
//定义FileOutputStream,用于创建ObjectOutputStream |
OutputStream outputStream = new FileOutputStream(file, true ); |
if (file.length() < 1 ){ |
objectOutputStream = new ObjectOutputStream(outputStream); |
} else { |
objectOutputStream = new MyObjectOutputStream(outputStream); |
} |
//把对象序列化到文件中 |
objectOutputStream.writeObject(obj); |
} catch (Exception e) { |
e.printStackTrace(); |
} finally { //强制关闭ObjectOutputStream |
try { |
objectOutputStream.close(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
|
|
} |
|
/** |
* 反序列化一个对象 |
* |
* @param path 要反序列的文件的路径 |
* @return 返回一个被反序列化的对象 |
*/ |
@SuppressWarnings ( "resource" ) |
public static Object unserializObject(String path){ |
//定义一个对象 |
Object obj = null ; |
//创建一个文件 |
File file = new File(path); |
|
try { |
//创建文件输入流 |
InputStream inputStream = new FileInputStream(file); |
//通过文件输入流创建ObjectInputStream |
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); |
//获取一个对象 |
obj = objectInputStream.readObject(); |
} catch (Exception e) { |
|
e.printStackTrace(); |
} |
|
//返回一个对象 |
return obj; |
} |
|
/** |
* 序列化一个集合 |
* 用户传一个集合和要序列化到的路径即可进行序列化 |
* @param collections 集合 |
* @param path 要序列化的路径 |
*/ |
public static <T> void serializList(List<T> collections,String path){ |
//创建文件 |
File file = new File(path); |
//判断文件是否存在 |
if (!file.exists()){ |
//如果文件不存在,就创建文件 |
try { |
file.createNewFile(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
//创建ObjectOutputStream |
ObjectOutputStream objectOutputStream = null ; |
|
try { |
//定义FileOutputStream,用于创建ObjectOutputStream |
OutputStream outputStream = new FileOutputStream(file, true ); |
if (file.length() < 1 ){ |
objectOutputStream = new ObjectOutputStream(outputStream); |
} else { |
objectOutputStream = new MyObjectOutputStream(outputStream); |
} |
//遍历集合,然后把对象序列化到文件中 |
for (T collection : collections){ |
objectOutputStream.writeObject(collection); |
} |
|
} catch (Exception e) { |
e.printStackTrace(); |
} finally { //强制关闭ObjectOutputStream |
try { |
objectOutputStream.close(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
|
} |
|
/** |
* 反序列化一个List集合 |
* 因为文件中有多个对象,如何在反序列化到末尾时,终止反序列化呢? |
* 这里有一个技巧,我们可以通过一个死循环,一直读知道抛出EOFException时就break退出循环 |
* @param path路径 |
* @return 返回一个集合 |
*/ |
@SuppressWarnings ( "unchecked" ) |
public static <T> List<T> unserializList(String path){ |
//创建一个文件 |
File file = new File(path); |
//创建一个List集合用于存储反序列化后的对象 |
List<T> collections = new ArrayList<T>(); |
//定义ObjectInputStream |
ObjectInputStream objectInputStream = null ; |
try { |
//创建文件输入流 |
InputStream inputStream = new FileInputStream(file); |
objectInputStream = new ObjectInputStream(inputStream); |
//获取一个对象 |
while ( true ){ |
try { |
//反序列化一个对象 |
T t = (T) objectInputStream.readObject(); |
//把该对象添加到集合中 |
collections.add(t); |
} catch (EOFException e) { |
break ; |
} |
} |
} catch (Exception e) { |
e.printStackTrace(); |
} finally { |
try { |
objectInputStream.close(); |
} catch (IOException e) { |
e.printStackTrace(); |
} |
} |
|
//返回一个对象 |
return collections; |
} |
|
} |
测试类: |
[java] view plaincopy |
/** |
* 测试类 |
* @author Liao |
* |
*/ |
public class SerializationTest { |
|
public static void main(String[] args) { |
|
Scanner in = new Scanner(System.in); |
|
boolean flag = true ; |
|
//创建集合用于存放实体对象 |
List<Person> persons = new ArrayList<Person>(); |
|
while (flag) { |
// 输入用户ID |
System.out.print( "请输入UID:" ); |
Integer uid = in.nextInt(); |
// 输入用户名 |
System.out.print( "请输入用户名:" ); |
String uname = in.next(); |
// 输入性别 |
System.out.print( "请输入性别:" ); |
String gender = in.next(); |
|
//把Person对象存到集合中 |
persons.add( new Person(uid, uname, gender.equals( "男" )? Gender.MAN : Gender.WOMEN)); |
|
// 是否继续 |
System.out.print( "是否继续? y/n:" ); |
|
String ifContinue = in.next(); |
|
if (ifContinue.equals( "n" )) { |
flag = false ; |
} |
} |
|
//序列化 |
SerializableUtil.serializList(persons, "D:\\liaozhongmin.txt" ); |
|
//反序列化 |
List<Person> collections = SerializableUtil.unserializList( "D:\\liaozhongmin.txt" ); |
System.out.println(collections); |
} |
|
} |
|