【Java基础】- IO流

2020-07-15  本文已影响0人  lconcise

IO流简介

IO流 用于设备间进行数据传输的操作。

IO流可以分为字节流和字符流,也可以分为输入流和输出流。

字符流 = 字节流 + 编码表
InputStreamReader、OutputStreamWriter 转换流

  1. 把对应的字节流转换字符流使用
  2. 可以指定码表进行读写文件的数据

字节流与字符流


image.png

字节流Demo

public class CopyDemo {

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        try {
            method01("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("字节流一次读取一个字节,花费时长:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        try {
            method02("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("字节流一次读取1024字节数组,花费时长:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        try {
            method03("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("高效字节流一次读取一个字节,花费时长:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        try {
            method04("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("高效字节流一次读取1024字节数组,花费时长:" + (System.currentTimeMillis() - start));

    }

    // 基本字节流一次读取一个字节
    public static void method01(String srcPath, String destPath) throws IOException {
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(destPath);

        int by;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }

        fis.close();
        fos.close();
    }

    // 基本字节流一次读取1024字节数组
    public static void method02(String srcPath, String destPath) throws IOException {
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(destPath);

        byte[] bys = new byte[1024];
        int len;
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }

        fis.close();
        fos.close();
    }

    // 高效字节流一次读取一个字节
    public static void method03(String strPath, String destPath) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(strPath));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));

        int by;
        while ((by = bis.read()) != -1) {
            bos.write(by);
        }

        bis.close();
        bos.close();
    }

    // 高效字节流一次读取1024字节数组
    public static void method04(String srcPath, String destPath) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcPath));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));

        byte[] bys = new byte[1024];
        int len;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }

        bis.close();
        bos.close();
    }
}

输出结果:

字节流一次读取一个字节,花费时长:4419
字节流一次读取1024字节数组,花费时长:6
高效字节流一次读取一个字节,花费时长:26
高效字节流一次读取1024字节数组,花费时长:3

字符流Demo

public class CopyDemo2 {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        try {
            method01("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("InputStreamReader字符流一次读取一个字符,花费时长:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        try {
            method02("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("InputStreamReader字符流一次读取1024字符数据,花费时长:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        try {
            method03("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("FileReader字符流一次读取1024字符数据,花费时长:" + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        try {
            method04("d:\\hello.pdf", "e:\\copy.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("BufferedReader高效字符流一次读取一行字符数据,花费时长:" + (System.currentTimeMillis() - start));
    }

    public static void method01(String srcPath, String destPath) throws IOException {
        InputStreamReader isR = new InputStreamReader(new FileInputStream(srcPath), "UTF-8");
        OutputStreamWriter osW = new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8");

        int by;
        while ((by = isR.read()) != -1) {
            osW.write(by);
        }

        isR.close();
        osW.close();
    }

    public static void method02(String srcPath, String destPath) throws IOException {
        InputStreamReader isR = new InputStreamReader(new FileInputStream(srcPath), "UTF-8");
        OutputStreamWriter osW = new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8");

        char[] chars = new char[1024];
        int len;
        while ((len = isR.read(chars)) != -1) {
            osW.write(chars, 0, len);
        }

        isR.close();
        osW.close();
    }

    public static void method03(String srcPath, String destPath) throws IOException {
        FileReader fr = new FileReader(srcPath);
        FileWriter fw = new FileWriter(destPath);

        char[] chars = new char[1024];
        int len;
        while ((len = fr.read(chars)) != -1) {
            fw.write(chars, 0, len);
        }

        fr.close();
        fw.close();
    }

    public static void method04(String srcPath, String destPath) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcPath), "UTF-8"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath), "UTF-8"));

        String line;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
        }

        br.close();
        bw.close();
    }
}

输出:

InputStreamReader字符流一次读取一个字符,花费时长:293
InputStreamReader字符流一次读取1024字符数据,花费时长:51
FileReader字符流一次读取1024字符数据,花费时长:33
BufferedReader高效字符流一次读取一行字符数据,花费时长:42
上一篇 下一篇

猜你喜欢

热点阅读