Java IO 流学习笔记

2021-03-16  本文已影响0人  啊啰哈嘿呀

Java IO 流学习笔记

1 什么是流

内存与存储设备之间传输数据的通道。

2 流的分类

2.1 按方向

2.2 按单位

字节(Byte)是计量单位,表示数据量多少,是计算机信息技术用于计量存储容量的一种计量单位,通常情况下一字节等于八位。
字符(Character)计算机中使用的字母、数字、字和符号,比如'A'、'B'、'$'、'&'等。

2.3 按功能

3 字节流抽象类

4.文件输入输出流

4.1 FileInputStream输入流

(1) read() 单字节读取

/**
     * 单字节读取
     * read(): 从该输入流读取下一个数据字节
     * @param filePath
     */
    public static void demo1(String filePath){
        try(FileInputStream fileInputStream = new FileInputStream(filePath)){
            int data;
            while ((data = fileInputStream.read()) != -1){
                System.out.print((char) data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

(2)read(byte[] b) 一次读取多个字节

/**
     * 一次读取多个字节
     *
     * read(byte[] b):从该输入流读取最多 byte.length个字节的数据到字节数组。
     * @param filePath
     */
    public static void demo2(String filePath){
        try(FileInputStream fileInputStream = new FileInputStream(filePath)){
            byte[] buf = new byte[3]; // 大小为3的缓存区
            int count = 0;
            while((count = fileInputStream.read(buf)) != -1){
                System.out.println(new String(buf, 0, count));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

(3) read(byte[] b, int off, int len)

 /**
     *一次读取多个字节
     *
     * read(byte[] b, int off, int len)
     * 从该输入流读取最多 len字节的数据到字节数组。
     * @param filePath
     */
    public static void demo3(String filePath){
        try(FileInputStream fileInputStream = new FileInputStream(filePath)){
            byte[] buf = new byte[3]; // 大小为3的缓存区
            int count = 0;
            while((count = fileInputStream.read(buf,0,buf.length)) != -1){
                System.out.println(new String(buf, 0, count));
            }

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

4.2 FileOutputStream文件输出流

public class FileOutputStreamDemo {
    public static void main(String[] args) {
        String filePath = "/Users/pandamig/IdeaProjects/Study/IOStudy/src/main/resources/ccc.txt";
        try(FileOutputStream out = new FileOutputStream(filePath)){
            out.write("hello word!".getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4.3 文件输入输出流案例

public class FileCopyDemo {
    public static void main(String[] args) {
        // 1.创建流,文件输入流,文件输出流
        String orgPath = "/Users/pandamig/IdeaProjects/Study/IOStudy/src/main/resources/ccc.txt";
        String tarPath = "/Users/pandamig/IdeaProjects/Study/IOStudy/src/main/resources/ddd.txt";

        try (
                FileInputStream in = new FileInputStream(orgPath);
                FileOutputStream out = new FileOutputStream(tarPath);
        ) {
       // 2 边读边写
            byte[] buf = new byte[1024];
            int count = 0;
            while ((count = in.read(buf)) != -1) {
                out.write(buf, 0, count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5 字节缓冲流

缓冲流:BufferedInputStream/ BufferedOutputStream

5.1 BufferedInputStream

public class BufferedInputStreamDemo {

    public static void main(String[] args) {
        String filePath = "/Users/pandamig/IdeaProjects/Study/IOStudy/src/main/resources/ccc.txt";
        try (
                FileInputStream fis = new FileInputStream(filePath);
                BufferedInputStream bis = new BufferedInputStream(fis)
        ) {
            // 2 读取
            int data = 0;
            while ((data = bis.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5.2 BufferedOutputStream

public class BufferedOutputStreamDemo {
    public static void main(String[] args) {
        String tarPath = "/Users/pandamig/IdeaProjects/Study/IOStudy/src/main/resources/eee.txt";
        try (
                FileOutputStream fos = new FileOutputStream(tarPath);
                BufferedOutputStream bos = new BufferedOutputStream(fos)
        ) {
            // 写入文件
            for (int i = 0; i < 10; i++) {
                bos.write("hello".getBytes());// 写入8k缓冲区
                bos.flush(); // 刷新到硬盘
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

6 对象流

ObjectOutputStream / ObjectInputStream

6.1 使用ObjectOutputStream实现对象的序列化

public class ObjectOutputStreamDemo {

    public static void main(String[] args) {
        // 1.创建对象流
        try (
                FileOutputStream fos = new FileOutputStream("/Users/pandamig/IdeaProjects/Study/IOStudy/src/main/resources/student.bin");
                ObjectOutputStream oos = new ObjectOutputStream(fos)
        ) {
            // 2.序列化
            Student student = new Student();
            student.setAge(14);
            student.setName("hello");

            oos.writeObject(student);

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

6.2 使用ObjectInputStream实现对象的反序列化

public class ObjectInputStreamDemo {
    public static void main(String[] args) {
        try (
                FileInputStream fis = new FileInputStream("/Users/pandamig/IdeaProjects/Study/IOStudy/src/main/resources/student.bin");
                ObjectInputStream ois = new ObjectInputStream(fis);
        ) {
            Student stu = (Student) ois.readObject();
            System.out.println(stu.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

6.3 注意事项

上一篇 下一篇

猜你喜欢

热点阅读