写一个Java原生的序列化和反序列化的DEMO。

需序列化的类:

package com.nicchagil.nativeserialize;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
private String userName; public User(Integer id, String userName) {
super();
this.id = id;
this.userName = userName;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public static long getSerialversionuid() {
return serialVersionUID;
} @Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result
+ ((userName == null) ? 0 : userName.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (userName == null) {
if (other.userName != null)
return false;
} else if (!userName.equals(other.userName))
return false;
return true;
} @Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + "]";
} }

工具类:

package com.nicchagil.nativeserialize;

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;
import java.io.Serializable; public class NativeSerializeTools { /**
* 序列化
* @param filePath 序列化的路径
* @param s 序列化的对象
*/
public static void write(String filePath, Serializable s) throws FileNotFoundException, IOException {
if (filePath == null || filePath.length() == 0) {
throw new RuntimeException("请传入序列化路径");
} if (s == null) {
throw new RuntimeException("请传入序列化对象");
} File f = new File(filePath); ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
oos = new ObjectOutputStream(fos);
oos.writeObject(s);
System.out.println("finish.");
} finally {
if (oos != null) {
oos.close();
}
if (fos != null) {
fos.close();
}
System.out.println("close the resource.");
}
} /**
* 反序列化
* @param filePath 反序列化的路径
* @return 反序列化的对象
*/
public static Object read(String filePath) throws ClassNotFoundException, FileNotFoundException, IOException {
if (filePath == null || filePath.length() == 0) {
throw new RuntimeException("请传入反序列化路径");
} File f = new File(filePath); ObjectInputStream ois = null;
FileInputStream fis = null;
Object o = null;
try {
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
o = ois.readObject();
System.out.println("finish.");
} finally {
if (ois != null) {
ois.close();
}
if (fis != null) {
fis.close();
}
System.out.println("close the resource.");
} return o;
} }

测试类:

package com.nicchagil.nativeserialize;

import java.io.FileNotFoundException;
import java.io.IOException; import org.junit.Assert;
import org.junit.Test; public class HowToUse { private User user = new User(100, "Nick Huang");
private String filePath = "d:/user.txt"; @Test
public void c1() throws FileNotFoundException, IOException {
NativeSerializeTools.write(filePath, user);
} @Test
public void c2() throws FileNotFoundException, IOException, ClassNotFoundException {
Object o = NativeSerializeTools.read(filePath); System.out.println(o);
Assert.assertTrue(user.equals(o));
} }

日志:

finish.
close the resource.
finish.
close the resource.
User [id=100, userName=Nick Huang]

【Java】Java原生的序列化和反序列化的更多相关文章

  1. java之多线程之一/序列化和反序列化

    线程安全: 如何自己手动创建一个线程 答:继承Thread类或实现Runnable接口 public class Mythread extends Thread{ @Override public v ...

  2. Java基础知识:序列化和反序列化

    一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通常存 ...

  3. java中对象的序列化和反序列化

    [对象的序列化和反序列化 ] 1.定义:序列化--将对象写到一个输出流中.反序列化则是从一个输入流中读取一个对象.类中的成员必须是可序列化的,而且要实现Serializable接口,这样的类的对象才能 ...

  4. windows环境下protobuf的java操作{编译,序列化,反序列化}

    google protocol buffer的使用和原理 概况: Protocol Buffers(也就是protobuf)是谷歌的语言中立的.平台中立的.可扩展的用于序列化结构化的数据: windo ...

  5. Java对象的serialVersion序列化和反序列化

    Java基础学习总结——Java对象的序列化和反序列化 一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种 ...

  6. ABAP,Java和JavaScript的序列化,反序列化

    ABAP 1. ABAP提供了一个工具类cl_proxy_xml_transform,通过它的两个方法abap_to_xml_xstring和xml_xstring_to_abap实现两种格式的互换. ...

  7. Java基础之数组序列化、反序列化 小发现(不知道 是不是有问题)

    结论:  数组,无论是否声明为transient,都是可以序列化.反序列化的. 测试情况如下: 1.两种类型的数组:int .String: 2 声明为transient  或者不做任何修饰:. 3. ...

  8. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. 【java学习笔记】序列化、反序列化

    序列化 是将对象的完整信息保存起来的过程(持久化).    序列化流:ObjectOutputStream 反序列化 是将对象进行还原的过程(反持久化).               反序列化流:Ob ...

随机推荐

  1. Test Android with QTP

    by Yaron Assa I have recently come across a plug-in to QTP that enables to automate tests on Android ...

  2. Intent跳转传list集合

    先把List<>改为ArrayList<> ArrayList<Good> list=new ArrayList<Good>(); Intent int ...

  3. java设置环境变量小工具

    unit MainUnit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...

  4. 安装qt5.3.2后,qtcreator在ubuntu 11.04无法启动的问题

    在官方网站下载.run文件安装后,qtcreator启动失败,然后找到命令行启动,失败原因如下: shr@shr-Sieyuan:~/Qt5.3.2/Tools/QtCreator/bin$ ./qt ...

  5. 危险的 SQL

    看下这个 SQL , 有什么问题 ? <update id="update" parameterType="CreativeGroupDO"> up ...

  6. DButil

    纲要: Properties prop = new Properties(); BasicDataSource ds = new BasicDataSorce(); Connection conn = ...

  7. linux下xargs命令用法详解 【转】

    转自:http://blog.chinaunix.net/uid-128922-id-289992.html xargs在linux中是个很有用的命令,它经常和其他命令组合起来使用,非常的灵活. xa ...

  8. Java中删除指定文件夹文件夹下面有内容也删除使用递归方案

    import java.io.File; import java.text.ParseException; import java.text.SimpleDateFormat; import java ...

  9. js,replace() 和 正则表达式(regular expression)

    repalce() 只能替换字符串中的匹配到的第一个字符或者字符串 正则表达式   替换多个字符或者字符串 注意:一些数字型的字符串使用replace() 时要确保是字符串,而不是数字. 转换方法: ...

  10. MEANIO

    sudo npm install -g bower sudo npm install -g meanio sudo bower cache clean --allow-root sudo mean i ...