Java IO流

1. 文件流

1.1. 文件字节流FileInputStream和FileOutputStream

  • FileInputStream和FileOutputStream是字节流,是节点流,数据源和目的地是文件
  • 复制文件需要分别创建一个输入流和输出流完成文件读写
  • 需要创建一个中转站,借助循环和中转站完成复制
  • 流使用完一定要关闭,这和垃圾回收没有关系

1.1.1. 复制文件(中转站是一个字节)

public class TestCopy1 {
    public static void main(String[] args) throws IOException {
        // 1. 创建一个输入流和输出流
        InputStream fis = new FileInputStream(new File("/e:/readme.txt"));
        OutputStream fos = new FileOutputStream(new File("e:/readme1.txt"));
        // 2. 使用输入流和输出流完成文件复制
        // 2.1. 准备一个中转站
        int n;
        // 2.2. 先读一个字节,读取一个字节,赋给n
        n = fis.read();
        while (n != -1) {
            // 2.3. 再写一个字节
            fos.write(n);
            // 2.4. 再读一个字节
            n = fis.read();
        }
        // 关闭输入流和输出流
        fis.close();
        fos.close();
    }
}
  • 缺点:中转站太小,速度慢效率低;复制更大文件时效果更明显;可以将中转站由一个字节变成一个字节数组,减少读写硬盘的次数
  • 问题:如果不是覆盖文件,而是追加内容如何实现?

1.1.2. 复制文件(中转站是一个字节数组)

public class TestCopy2 {
    public static void main(String[] args) throws IOException {
        // 1. 创建流
        InputStream fis = new FileInputStream("e:/readme.txt");
        // 追加而不是覆盖
        OutputStream fos = new FileOutputStream("e:/readme2.txt", true);
        // 2. 使用流
        // 2.1. 准备一个中转站
        byte[] buf = new byte[1024];
        // 2.2. 使用输入流从数据源读取数据到中转站,数据在数组中转站中,返回的真正读取的字节数
        int len = fis.read(buf);
        while (len != -1) {
            // 2.3. 使用输出流把中转站数据写到目的文件
            fos.write(buf, 0, len);
            // 2.4. 再读一次
            len = fis.read(buf);
        }
        // 3. 关闭流
        fis.close();
        fos.close();
    }
}

1.2. 文件字符流FileReader和FileWriter

  • FileReader和FileWriter是字符流,节点流,数据源和目的地是文件

1.2.1. 复制文件(中转站是一个字符)

public class TestCopy3 {
    public static void main(String[] args) throws IOException {
        Reader fr = new FileReader("e:/readme.txt");
        Writer fw = new FileWriter("e:/readme3.txt");
        // 使用输入流读取一个字符char,而不是字节
        int n = fr.read();
        while (n != -1) {
            System.out.println((char) n);
            fw.write(n);
            n = fr.read();
        }
        fr.close();
        fw.close();
    }
}

1.2.2. 复制文件(中转站是一个字符数组,并进行异常处理)

public class TestCopy4 {
    public static void main(String[] args) {
        Reader fr = null;
        Writer fw = null;
        try {
            fr = new FileReader("e:/readme.txt");
            fw = new FileWriter("e:/readme4.txt");
            char[] cbuf = new char[1024];
            int len = fr.read(cbuf);
            while (len != -1) {
                fw.write(new String(cbuf, 0, len));
                len = fr.read(cbuf);
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(fr != null) fr.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fw != null) fw.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 其实只有字节流,没有字符流。字符流的底层还是字节流,进行了封装转换,使开发者可以更简单的来处理非英文字符
  • 字节流可以完成所有类型文件的复制(文本、音频、视频、图片、chm);字符流只可以完成文本文件的复制(txt、java)doc不是文本文件;字符流一般用来处理包含中文的文本文件
  • 异常处理的分析:创建流、使用流要使用一次try-catch语句,关闭流要分开进行异常处理

2. 缓冲流

2.1. 缓冲字节流BufferedInputStream和BufferedOutputStream

  • 复制文件(使用缓冲流字节流提高效率)
public class TestCopy5 {
    public static void main(String[] args) throws IOException {
        InputStream fis = new FileInputStream(new File("e:/readme.CHM"));
        OutputStream fos = new FileOutputStream(new File("e:/readme1.CHM"));
        // 默认输入缓冲区大小8192
        BufferedInputStream bis = new BufferedInputStream(fis);
        // 默认输出缓冲区大小8192
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int n;
        n = bis.read();
        while (n != -1) {
            bos.write(n);
            n = bis.read();
        }
        bis.close();
        bos.close();
    }
}
  • 缓冲流的原理
    在这里插入图片描述
  • 只要关闭高层流即可,底层流不用手动关闭;因为高层的关闭方法就是把底层流关闭
  • 刷新输出缓冲区(让缓冲区内容写入硬盘,保持一致)
    • 满了就自动刷新
    • bos.close():先flush,再关闭
    • 手动刷新flush()

2.2. 缓冲字符流BufferedReader和BufferedWriter

  • 问题:之前的文件读写都是按照字节、字符或数组来实现的,对于文本文件而言,能否按照行来读写呢?
  • 解决:提供了BufferedReader和BufferedWriter实现按行读写

复制文件(按行读写)

public class TestCopy6 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("e:/readme.log"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("e:/readme1.log"));
        // 使用两个流完成按行读取的功能,中转站就是一个字符串,存储一行数据
        String str = br.readLine();
        while (str != null) {
            bw.write(str);
            // 不同操作系统中换行符是不同的
            bw.newLine();
            str = br.readLine();
        }
        br.close();
        bw.close();
    }
}
  • 总结
    1. BufferedReader和BufferedWriter的优点:
      • 速度快
      • 简化编程
    2. readLine()底层的原理:一个一个字符的读取,append()放入到StringBuilder(或char[])中,遇到换行符,将StringBuilder(char[])转换成String并返回
    3. 不同操作系统换行符不同
      • Unix:每行结尾只有换行\n
      • Windows:每行结尾是回车+换行:\r\n
      • Mac:每行结尾回车:\r

3. 数据流DataInputStream和DataOutputStream

之前使用文件流、缓冲流读取文件只能按照字节、数组方式读取,最方便的也是按行读取,要想很方便的实现对各种基本类型和引用类型数据的读写,并保留其本身的类型;需要使用DataInputStream、DataOutputStream和对象流ObjectInputStream、ObjectOutputStream,优势就是提供了方便操作各种数据的方法,直接调用,简单方便

  • 注意
    • 只有字节流,没有字符流
    • 都是处理流,不是节点流
    • 数据流只能操作基本数据类型和字符串,对象流还可以操作对象
    • 写入的是二进制数据,无法直接通过记事本等查看
    • 写入的数据需要使用对应的输入流来读取
public class TestDataStream {
    public static void main(String[] args) throws Exception {
        write();
        read();
    }
    public static void write() throws Exception {
        OutputStream fos = new FileOutputStream("e:/test.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeInt(1);
        dos.writeDouble(3.14);
        dos.writeChar('A');
        dos.writeBoolean(true);
        dos.writeUTF("wyb");
        dos.close();
    }
    public static void read() throws Exception {
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File("e:/test.txt"))));
        System.out.println(dis.readInt()); // 1
        double d = dis.readDouble(); 
        System.out.println(d); // 3.14
        System.out.println(dis.readChar()); // A
        System.out.println(dis.readBoolean()); // true
        System.out.println(dis.readUTF()); // wyb
        dis.close();
    }
}

3.2. 对象流ObjectInputStream和ObjectOutputStream

public class TestObjectStream  {
    public static void main(String[] args) throws Exception {
        write();
        read();
    }
    public static void write() throws Exception {
        OutputStream fos = new FileOutputStream("e:/test.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeInt(1);
        oos.writeDouble(3.14);
        oos.writeChar('A');
        oos.writeBoolean(true);
        oos.writeUTF("Hello");
        oos.writeObject(new Date());
        oos.writeObject(new Student(1, "111", 23, 333.3));
        oos.close();
    }
    public static void read() throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File("/Users/dxm/Desktop/code/other/java/patice/test.txt"))));
        System.out.println(ois.readInt()); // 1
        double d = ois.readDouble();
        System.out.println(d); // 3.14
        System.out.println(ois.readChar()); // A
        System.out.println(ois.readBoolean()); // true
        System.out.println(ois.readUTF()); // Hello
        Object date = (Date)ois.readObject(); 
        System.out.println(date); // Wed Aug 07 11:28:46 CST 2024
        // System.out.println(ois.readObject());
        ois.close();
    }
}

注意:使用对象流读写引用类型的数据,需要相应类实现Serializable接口,否则会提示异常,提示没有序列化,比如:java.io.NotSerializbleException; com.wyb.Student

3.3. 序列化和反序列化

  1. 序列化和反序列化
    • 序列化:Serialization:将对象的状态信息转换为可存储或传输的形式的过程。对象(内存)—> 字节数组 字符序列(外存、网络)
    • 反序列化:DeSerialization,字节数组 字节序列(外存、网络)—> 对象(内存)
  2. 什么时候需要序列化和反序列化:存储或传输:比如存储到外存(硬盘)中,传输到网络
  3. 如何实现序列化和反序列化:相应的类要实现Serializable接口
public class Student implements Serializable {
}
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(new Student(1, "111", 22, 333.3));
ObjecInputStream ois = new ObjectInputStream(bis);
Student stu = (Student)ois.readObject();
  1. 序列化的细节
  • 为什么序列化接口没有任何方法(查看ObjectInputStream源码)
  • static属性不参与序列化
  • 如果不希望某个属性参与序列化,需要使用transient修饰
  • Exception in thread “main” java.io.InvalidClassException: com.bjsxt.entity.Student; local class incompatible: stream classdesc serialVersionUID = 5954363181006202290, local class serialVersionUID = -1877375566195009060
    • 解决方法:给出一个固定的序列化版本号serialVersionUID
  • 使用对象流把一个对象写到文件时,不仅保证该对象是序列化的,而且该对象的成员对象也必须是可序列化的。

4. 其他流

4.1. 其他流

  1. 打印流:PrintStream和PrintWriter,只有输出流,没有输入流;System.out、System.err是PrintStream的实例变量
  2. 转换流:InputStreamReader和OutputStreamWriter,实现字节流到字符流的转换,是适配器设计模式的应用。只能从字节流到字符流的转换,可以带来处理字符的便利。
  3. 字节数组流:ByteArrayInputStream和ByteArrayOutputStream,是节点流,数据源是字节数组,可以实现各种基本和引用数据类型与字节数组之间的转换
  4. JavaIO流的设计使用了装饰模式,动态组装流,可以减少子类的数量,是继承的一种替代方案
OutputStream fos = new FileOutputStream("e:/readme.txt");
// 提高速度
BufferedOutputSteam bos = new BufferedOutputStream(fos);
// 简化操作
DataOutputStream dos = new DataOutputStream(bos);
public class Test {
    public static void main(String[] args) throws IOException {
        PrintStream ps;
        PrintWriter pw;
        // println强大作用:不管什么类型,都转成字符串并输出
        System.out.println();
        System.err.println();
        // 接受键盘输入的一行数据并输出,将字节输入流InputStreamReader转换为字符输入流Reader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new FileWriter("e:/test.txt"));
        // 使用两个流完成按行读取,中转站就是一个字符串,存储一行数据
        String str = br.readLine();
        while (!"bye".equals(str)) { 
            bw.write(str);
            bw.newLine();
            str = br.readLine();
        }
        bw.close();
        FileInputStream fis;
        FileOutputStream fos;
        ByteArrayInputStream bais;
        ByteArrayOutputStream baos;
    }
}

在这里插入图片描述

4.2. 复制文件夹

Q1: 使用字节流还是字符流
A1: 使用字节流,可能有图片、视频、音频等二进制文件
Q2: 如何提高复制速度
A2: BufferedInputStream和BufferedOutputStream; byte[] buf = new byte[1024];
Q3: 涉及的技能点
A3:

  1. IO流:文件的复制
  2. 递归:各级文件夹和文件的递归复制
  3. File类:文件夹的定义和创建

Q4: 问题的迭代
A4:

  1. 复制一个文件
  2. 复制一个文件夹下所有的文件(不包括子文件夹)
  3. 复制一个文件夹下所有的文件和子文件夹,从而完成文件夹的复制

4.2.1. 复制一个文件

public class TestDirCopy {
    public static void main(String[] args) {
        copyFile("e:/test.txt", "e:/test1.txt");
    }
    public static void copyFile(String sourceFileName, String targetFileName) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(new File(sourceFileName)));
            bos = new BufferedOutputStream(new FileOutputStream(targetFileName));
            byte[] buf = new byte[1024];
            int len = bis.read(buf);
            while(len != -1) {
                bos.write(buf, 0, len);
                len = bis.read(buf);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4.2.2. 复制一个文件文件夹(含子文件夹)

public class TestDirCopy3 {
    public static void main(String[] args) {
        copyDir("e:/video", "e:/video1");
    }
    public static void copyDir(String sourceDirName, String targetDirName) {
        // 创建一个目的文件夹
        File targetDir = new File(targetDirName);
        if(!targetDir.exists()) {
            targetDir.mkdirs();
        }
        // 复制源文件夹下的所有文件到目的文件夹
        File sourceDir = new File(sourceDirName);
        // 文件夹下所有的文件和子文件夹
        File[] files = sourceDir.listFiles();
        for(File file : files) {
            // 如果是文件,就复制
            if(file.isFile()) {
                copyFile(sourceDirName + '/' + file.getName(), targetDirName + '/' + file.getName());
            }
            // 如果是文件夹,就递归复制
            if(file.isDirectory()) {
                copyDir(sourceDirName + '/' + file.getName(), targetDirName + '/' + file.getName());
            }
        }
    }
}
  • 22
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qi妙代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值