Java序列化对象的存储和读取

来源:互联网 发布:233网校软件 编辑:程序博客网 时间:2024/06/04 18:06

本文通过一个示例讲解java序列化对象的存储和读取。

一.基础知识

(一)存储文件使用的是:ObjectOutputStream和它的方法writeObject来进行写入对象,可以写入任何类型的数据

(二)读取文件使用ObjectInputStream和它的readObject来进行对象的读取,按顺序读取写入的数据。

(三)存储的对象必须实现接口Serializable。

二.程序示例

本示例展示多种数据类型的写入和读取。

(一)题目

1.创建一个文件user.txt.
2.向文件写入序列化对象User
属性:account,值:danny 属性:password,值:123456 属性:age,值:26 属性:high,值:172.5
3.向文件写入集合对象 (存放几种简单的数据类型)
4.向文件写入写入几个基本数据
5.从文件user.txt中读出所有的数据,打印出来

(二)程序分析:

1.创建文件使用file.createNewFile();
2.使用ObjectOutputStream和它的方法writeObject来进行写入对象。
3.使用ObjectInputStream和它的readObject来进行对象的读取。
4.读取信息的顺序必须和存放的顺序是一致的,否则得不到对应的数据。

(三)程序代码

1.实现序列化的类

package com.java.test3;import java.io.Serializable;public class User implements Serializable {// 实现序列化    String account = "";    String password = "";    int age = 0;    double height = 0;    // 重写构造方法,方便数的传入    public User(String account, String password, int age, double height) {        super();        this.account = account;        this.password = password;        this.age = age;        this.height = height;    }    // 重写toString方法,方便显示    @Override    public String toString() {        return "account=" + account + ", password=" + password + ", age=" + age                + ", height=" + height + "";    }}

2.主方法的代码

package com.java.test3;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.OutputStream;import java.util.ArrayList;import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;public class IOTest {    public static void main(String[] args) {        // 在D盘创建一个文件user.txt,使用File的方法        File file = new File("D:", "user.txt");//指定文件路径        try {            file.createNewFile();// 创建文件        } catch (IOException e1) {        }        if (file.exists()) {            //文件的写入             ForWrite(file);                     //文件的读取            ForRead(file);        } else {            System.out.println("文件不存在!");        }    }    private static void ForRead(File file) {        // 文件的读取        ObjectInputStream ois = null;        try {            ois = new ObjectInputStream(new FileInputStream(file));            //数据的读取。每一次读取都会把相应的游标往下移动一位            //在输入流中读取对象            User temp=(User) ois.readObject();            System.out.println("读取的对象内容为:");            System.out.println(temp);             //在输入流中读取集合            ArrayList<Object>list=(ArrayList<Object>) ois.readObject();            System.out.println("打印集合数据信息");            System.out.println(list);//打印list里面的元素            //在输入流中读取基本数据            Object o1=ois.readObject();            Object o2=ois.readObject();            System.out.println("打印基本数据信息");            System.out.println("o1="+o1+"  ,o2="+o2);        } catch (Exception e) {            System.out.println(e.getMessage() + "错误!");        }finally{            try {                ois.close();// 关闭输入流            } catch (IOException e) {            }        }    }    private static void ForWrite(File file) {        ObjectOutputStream oos =null;               try {            // 把对象写入到文件中,使用ObjectOutputStream            oos = new ObjectOutputStream(                    new FileOutputStream(file));            // 创建对象            User user = new User("danny", "123465", 26, 172.5);            // 把对象写入到文件中            oos.writeObject(user);            //创建一个集合            ArrayList<Object> list=new ArrayList<>();            list.add(11);            list.add("是光棍数字!");            // 把集合对象写入到文件中            oos.writeObject(list);            //添加基本信息            oos.writeObject(22222222);            oos.writeObject("荷包蛋!");            System.out.println("写入文件完毕!");        } catch (IOException e) {            System.out.println(e.getMessage() + "错误!");        }finally{            try {                oos.close();//关闭输出流            } catch (IOException e) {            }        }    }}

运行结果:

x1
       可以看到程序可以写入和读取任何类型的数据。

       但是在实际中一般不会写入很多中类型的东西,一般都是写入多个对象,然后以此读取多个对象的信息。这里只是为了显示它的作用效果,才存储各种数据类型。

5 0