风也温柔

计算机科学知识库

java 序列化 写入mysql JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式)

  什么是序列

  java中的序列化()机制能够将一个实例对象的状态信息写入到一个字节流中java 序列化 写入mysql,使其可以通过进行传输、或者持久化存储到数据库或文件系统中;然后在需要的时候java 序列化 写入mysqljava 序列化 写入mysql JAVA序列化与反序列化三种格式存取(默认格式、XML格式、JSON格式),可以根据字节流中的信息来重构一个相同的对象。序列化机制在java中有着广泛的应用,EJB、RMI等技术都是以此为基础的。

  正确使用序列化机制

  一般而言,要使得一个类可以序列化,只需简单实现 java.io. 接口即可( 还要实现无参数的构造方法 )。该接口是一个标记式接口,它本身不包含任何内容,实现了该接口则表示这个类准备支持序列化的功能。如下例定义了类,并声明其可以序列化。

  <pre style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">public class Person implements Serializable { private static final long serialVersionUID = 1L; private String name;; private int age; public Person(){ } public Person(String str,int n){ System.out.println("Inside Person's Constructor"); name = str; age = n; } public String getName() { return name; } public int getAge() { return age; } }</pre>

  下面为三种格式转换的代码:

  java 序列化 写入mysql_mysql如何创建序列_java 批量写入mysql

  1.默认格式.

  <pre style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">public class SerializeToFlatFile { public static void main(String[] args) { SerializeToFlatFile ser = new SerializeToFlatFile(); ser.savePerson(); ser.restorePerson(); } public void savePerson(){ Person myPerson = new Person("Jay", 24); try{ FileOutputStream fos = new FileOutputStream("E:\person.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); System.out.println("Person--Jay,24---Written"); oos.writeObject(myPerson); oos.flush(); oos.close(); }catch(Exception e){ e.printStackTrace(); } } @SuppressWarnings("resource") public void restorePerson(){ try{ FileInputStream fls = new FileInputStream("E:\person.txt"); ObjectInputStream ois = new ObjectInputStream(fls); Person myPerson = (Person)ois.readObject(); System.out.println("n---------------------n"); System.out.println("Person --read:"); System.out.println("Name is:"+myPerson.getName()); System.out.println("Age is :"+myPerson.getAge()); }catch(Exception e){ e.printStackTrace(); } } }</pre>

  输出结果: sr test.. I ageL namet Ljava/lang/;xp t Jay

  2.XML格式

  <pre style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);"> public class SerializeXML { public static void main(String[] args) { SerializeXML ser = new SerializeXML(); ser.serializeToXml(); ser.deSerializeFromXml(); } public void serializeToXml(){ Person[] myPersons = new Person[2]; myPersons[0] = new Person("Jay", 24); myPersons[1] = new Person("Tom", 23); XStream xStream = new XStream(); xStream.alias("Person", Person.class); try{ FileOutputStream foStream = new FileOutputStream("E:\persons.xml"); xStream.toXML(myPersons,foStream); }catch(Exception e){ e.printStackTrace(); } } public void deSerializeFromXml(){ XStream xStream = new XStream(); xStream.alias("Person", Person.class); Person[] myPersons = null; try{ FileInputStream flStream = new FileInputStream("E:\persons.xml"); myPersons = (Person[])xStream.fromXML(flStream); if(myPersons!=null){ for(Person person:myPersons){ System.out.println(person.getName()); System.out.println(person.getAge()); } } }catch(Exception e){ e.printStackTrace(); } } }</pre>

  输出结果:

  java 序列化 写入mysql_mysql如何创建序列_java 批量写入mysql

  <pre style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);"> Jay 24 Tom 23 </pre>

  3.JSON格式

  <pre style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);"> public class SerializeJSON { public static void main(String[] args) { SerializeJSON serializeJSON = new SerializeJSON(); serializeJSON.writeJSON(); serializeJSON.readJSON(); } public void writeJSON(){ XStream xStream = new XStream(new JettisonMappedXmlDriver()); Person person = new Person("geniushehe", 16); try { FileOutputStream fos = new FileOutputStream("E:\json.js"); xStream.setMode(XStream.NO_REFERENCES); xStream.alias("Person", Person.class); xStream.toXML(person, fos); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void readJSON(){ XStream xStream = new XStream(new JettisonMappedXmlDriver()); Person person = null; try { FileInputStream fis = new FileInputStream("E:\json.js"); xStream.setMode(XStream.NO_REFERENCES); xStream.alias("Person", Person.class); person = (Person)xStream.fromXML(fis); System.out.println(person.getName()); System.out.println(person.getAge()); } catch (FileNotFoundException e) { e.printStackTrace(); } } }</pre>

  输出结果:

  <pre style="color: rgb(89, 89, 89); margin: 0px; padding: 0px; background: none 0% 0% / auto repeat scroll padding-box border-box rgba(0, 0, 0, 0);">{"Person":{"name":"geniushehe","age":16}}</pre>

  文章来源:https://www.cfanz.cn/resource/detail/kXrBpmPwPAJvR