Java中如何实现一个简单的原生序列化与反序列化示例代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计930个文字,预计阅读时间需要4分钟。
本文简要介绍了Java原生序列化和反序列化代码实例。通过示例代码,展示了序列化和反序列化的基本操作,内容非详尽,适合对Java序列化有一定了解的学习者或工作者参考。需要进一步学习的读者,可参考相关资料进行深入研究。以下是一个Java原生序列化的简单示例:
javaimport java.io.*;
public class SerializationExample { public static void main(String[] args) { // 创建一个对象 Student student=new Student(张三, 20);
// 序列化 try (ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(student.dat))) { out.writeObject(student); System.out.println(对象序列化成功!); } catch (IOException e) { e.printStackTrace(); }
// 反序列化 try (ObjectInputStream in=new ObjectInputStream(new FileInputStream(student.dat))) { Student deserializedStudent=(Student) in.readObject(); System.out.println(对象反序列化成功!); System.out.println(姓名: + deserializedStudent.getName() + ,年龄: + deserializedStudent.getAge()); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } }}
class Student implements Serializable { private String name; private int age;
public Student(String name, int age) { this.name=name; this.age=age; }
public String getName() { return name; }
public int getAge() { return age; }}
以上代码展示了如何使用Java原生序列化和反序列化技术将一个对象存储到文件中,并在需要时重新读取。这为Java开发者提供了一个基础的了解,对于更深入的学习,建议参考相关书籍或在线资源。
这篇文章主要介绍了Java原生序列化和反序列化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
写一个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]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。
本文共计930个文字,预计阅读时间需要4分钟。
本文简要介绍了Java原生序列化和反序列化代码实例。通过示例代码,展示了序列化和反序列化的基本操作,内容非详尽,适合对Java序列化有一定了解的学习者或工作者参考。需要进一步学习的读者,可参考相关资料进行深入研究。以下是一个Java原生序列化的简单示例:
javaimport java.io.*;
public class SerializationExample { public static void main(String[] args) { // 创建一个对象 Student student=new Student(张三, 20);
// 序列化 try (ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(student.dat))) { out.writeObject(student); System.out.println(对象序列化成功!); } catch (IOException e) { e.printStackTrace(); }
// 反序列化 try (ObjectInputStream in=new ObjectInputStream(new FileInputStream(student.dat))) { Student deserializedStudent=(Student) in.readObject(); System.out.println(对象反序列化成功!); System.out.println(姓名: + deserializedStudent.getName() + ,年龄: + deserializedStudent.getAge()); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } }}
class Student implements Serializable { private String name; private int age;
public Student(String name, int age) { this.name=name; this.age=age; }
public String getName() { return name; }
public int getAge() { return age; }}
以上代码展示了如何使用Java原生序列化和反序列化技术将一个对象存储到文件中,并在需要时重新读取。这为Java开发者提供了一个基础的了解,对于更深入的学习,建议参考相关书籍或在线资源。
这篇文章主要介绍了Java原生序列化和反序列化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
写一个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]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

