码迷,mamicode.com
首页 > 其他好文 > 详细

数据流:DataOutputStream与DataInputStream的使用

时间:2019-11-03 23:38:39      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:output   int   download   data   over   代码   family   turn   setw   

看这两个类的名字就不难猜测出它们的类关系图。

DataOutputStream: 主要是一些writeXxx()操作,写出, 相当于序列化

DataInputStream: 主要是一些readXxx()操作,写入, 相当于反序列化

一般情况下,两者会成对出现。 在使用过程中要特别注意, 一个writeXxx() 对应着一个readXxx(), 千万不要用readInt()去对应writeBoolean().

说白了,就是序列化与反序列化要匹配。

下面举个例子,将Student对象的属性序列化后保存到文件中,然后反序列还原,示例代码如下:

 

public class Student {
    private int id;
    private String name;
    private long age;
    private boolean gender;

    public Student(int id, String name, long age, boolean gender) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public long getAge() {
        return age;
    }

    public void setAge(long age) {
        this.age = age;
    }

    public boolean isGender() {
        return gender;
    }

    public void setGender(boolean gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", gender=" + gender +
                ‘}‘;
    }
}

 

 

 

 @Test
    public void testDataStream() throws IOException {
        // 创建一个Student对象
        Student student = new Student(1, "郑钦锋", 100, true);
        // 创建一个DataOutputStream对象
        OutputStream outputStream = new FileOutputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt");
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        // 对java的8种基本数据类型进行write操作,其实就相当于序列化操作
        dataOutputStream.writeInt(student.getId());
        dataOutputStream.writeUTF(student.getName());
        dataOutputStream.writeLong(student.getAge());
        dataOutputStream.writeBoolean(student.isGender());
        // 关流
        dataOutputStream.close();


        // 读取文件,进行反序列化操作
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt");
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        int id = dataInputStream.readInt();
        String name = dataInputStream.readUTF();
        long age = dataInputStream.readLong();
        boolean gender = dataInputStream.readBoolean();

        // 还原这个student对象
        Student retStudent = new Student(id, name, age, gender);
        System.out.println(retStudent);  // Student{id=1, name=‘郑钦锋‘, age=100, gender=true}
        dataInputStream.close();
    }
}

 

如果序列化与反序列化不匹配,就是个悲剧,请看下面的示例:

  @Test
    public void testDataStream() throws IOException {
        // 创建一个Student对象
        Student student = new Student(1, "郑钦锋", 100, true);
        // 创建一个DataOutputStream对象
        OutputStream outputStream = new FileOutputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt");
        DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
        // 对java的8种基本数据类型进行write操作,其实就相当于序列化操作
        dataOutputStream.writeLong(student.getAge());
        dataOutputStream.writeBoolean(student.isGender());
        dataOutputStream.writeUTF(student.getName());
        dataOutputStream.writeInt(student.getId());
        // 关流
        dataOutputStream.close();


        // 读取文件,进行反序列化操作
        FileInputStream inputStream = new FileInputStream("E:\\360downloads\\wpcache\\srvsetwp\\student.txt");
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        int id = dataInputStream.readInt();
        String name = dataInputStream.readUTF();
        long age = dataInputStream.readLong();
        boolean gender = dataInputStream.readBoolean();

        // 还原这个student对象
        Student retStudent = new Student(id, name, age, gender);
        System.out.println(retStudent);  // Student{id=0, name=‘‘, age=28148597348991889, gender=true}
        dataInputStream.close();
    }

 

所以,write序列化的顺序是: int(id) ---> string(name) ---> long(age)----> boolean(gender);

read反序列化的顺序一定要是: int(id) ---> string(name) ---> long(age)----> boolean(gender);

 

数据流:DataOutputStream与DataInputStream的使用

标签:output   int   download   data   over   代码   family   turn   setw   

原文地址:https://www.cnblogs.com/z-qinfeng/p/11789786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!