数据流

DataInputStream(字节输入流的实现类)、DataOutputStream(字节输出流的实现类),下面例子先看输出,再看输入,因为输入和输出的类型顺序要一致

DataInputStream

读取数据输出流写出去的数据、

IO流:数据流、序列化流、IO框架_System

public class DataInputStreamTest2 {
    public static void main(String[] args) {
        try (DataInputStream dis = new DataInputStream(new FileInputStream("io-app2/src/a10.txt"))) {

            int i = dis.readInt();
            System.out.println(i);

            double d = dis.readDouble();
            System.out.println(d);

            boolean b = dis.readBoolean();
            System.out.println(b);

            String rs = dis.readUTF();
            System.out.println(rs);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

DataOutputStream

允许把数据和其类型一并写出去。

IO流:数据流、序列化流、IO框架_序列化_02

public class DataOutputStreamTest1 {
    public static void main(String[] args) {
        try {
            // 1. 创建一个数据输出流包裹低级的字节输出流
            DataOutputStream dos = 
                new DataOutputStream(new FileOutputStream("io-app2/src/itheima10out.txt"));
            
            dos.writeInt(97);
            dos.writeDouble(99.5);
            dos.writeBoolean(true);
            dos.writeUTF("程序员");
            
            dos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

序列化流

对象序列化:把java对象写进到文件。

对象反序列化:把文件中的Java对象读出来。

IO流:数据流、序列化流、IO框架_输出流_03

ObjectOutputStream(对象字节输出流)

把Java对象序列化:把java对象写进到文件。

IO流:数据流、序列化流、IO框架_序列化_04

如果对象需要序列化,则需要实现Serializable接口
//transient关键字修饰的变量不会被序列化

构造器

import java.io.Serializable;
//如果对象需要序列化,则需要实现Serializable接口
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private String loginName;
    private String userName;
    private int age;
    //transient关键字修饰的变量不会被序列化
    private transient String passWord;


    public User() {
    }

    public User(String loginName, String userName, int age, String passWord) {
        this.loginName = loginName;
        this.userName = userName;
        this.age = age;
        this.passWord = passWord;
    }
public class Test10ObjectOutputStream {
    public static void main(String[] args) {
        try {
            // 2. 创建一个对象字节输出流包装原始的字节输出流。
            ObjectOutputStream oos =
                new ObjectOutputStream(new FileOutputStream("io-app2/src/a11out.txt"));
            
            // 1. 创建一个Java对象。
            User u = new User("admin", "张三", 32, "666888xyz");
            
            // 3. 序列化对象到文件中
            oos.writeObject(u);
            System.out.println("序列化对象成功!!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ObjectInputputStream(对象字节输入流)

反序列化:把文件中的Java对象读出来。

IO流:数据流、序列化流、IO框架_序列化_05

public class Test20ObjectInputStream {
    public static void main(String[] args) {
        try {
            // 1. 创建一个对象字节输入流管道,包装低级的字节输入流与源文件接口
            ObjectInputStream ois = 
                new ObjectInputStream(new FileInputStream("io-app2/src/a11out.txt"));
            
            User u = (User) ois.readObject();
            System.out.println(u);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如果要一次序列化多个对象,可以使用ArrayList集合存储多个对象,然后对集合进行序列化。ArrayList集合已经实现了序列化接口。

class MyObject implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;

    public MyObject(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "MyObject{name='" + name + "'}";
    }
}

public class SerializeExample {
    public static void main(String[] args) {
        ArrayList<MyObject> myObjectList = new ArrayList<>();
        myObjectList.add(new MyObject("Object1"));
        myObjectList.add(new MyObject("Object2"));
        myObjectList.add(new MyObject("Object3"));

        try (FileOutputStream fileOut = new FileOutputStream("objects.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut)) {

            out.writeObject(myObjectList);
            System.out.println("Serialization successful");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

IO框架

框架:

为了解决某类问题,编写的一套类、接口等,相当于一个半成品,大多数框架都是第三方研发的。

优点:

在框架基础上开发,可以得到优秀的软件架构,提高开发效率。

格式:

一般把类、接口等编译成class形式,再压缩成.jar结尾的文件发行出去。

IO框架:

封装了Java提供的对文件、数据进行操作的代码,对外提供了更简单的方式对文件进行操作,对数据进行读写。

Commons-io

apache的一个io小框架。

IO流:数据流、序列化流、IO框架_输出流_06

IO流:数据流、序列化流、IO框架_System_07

搜索Commons-io进入官网下载jar包,在项目中整合即可使用。

IO流:数据流、序列化流、IO框架_System_08

因为这样搞得Java很没面子,所以后续它也提供了一些代码以供使用。

//java 提供的原生一行代码
Files.copy(Path.of("io-app2\\src\\itheima01.txt"), Path.of("io-app2\\src\\b.txt"));

System.out.println(Files.readString(Path.of("io-app2\\src\\a01.txt")));