美文网首页
【Java基础】- IO流

【Java基础】- IO流

作者: lconcise | 来源:发表于2020-07-15 22:20 被阅读0次

IO流简介

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

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

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

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

字节流与字符流

  • 字节流
    • 字节输入流
      • InputStream 抽象类
        • int read() : 一次读取一个字节
        • int read(byte[] bys) : 一次读取一个字节数组
      • FileInputStream
      • BufferedInputStream
    • 字节输出流
      • OutputStream 抽象类
        • void write() : 一次写一个字节
        • void write(byte[] bys,int index,int len) : 一次写字节数组的一部分
      • FileOutputStream
      • BufferedOutputStream
  • 字符流
    • 字符输入流
      • Reader 抽象类
        • int Read() : 一次读取一个字符
        • int Read(char[] chars) : 一次读取一个字符数组
      • InputStreamReader
      • FileReader
      • BufferedReader
        • String readLine() : 一次读取一行数据
  • 字符输出流
    • Writer 抽象类
      • void write(int ch) : 一次写一个字符
      • void write(char[] chars): 一次写一个字符数组
    • OutputStreamWriter
    • FileWriter
    • BufferedWriter
      • void newLine(): 写一个换行符
      • void write(String line)

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

相关文章

  • java IO入门笔记

    1.java IO流的概念,分类,类图 1.1. java IO 流的概念 java的io是实现输入和输出的基础,...

  • Java IO详解

    1 Java IO流的概念,分类 1.1 Java IO流的概念 java的IO是实现输入和输出的基础,可以方便的...

  • java基础之IO流

    IO流上:概述、字符流、缓冲区(java基础) IO流结构图 FilterInputStream、FilterOu...

  • Java之IO流详解

    title: Java之IO流详解tags: Java IO流categories: Java IO流 大多数应用...

  • Java基础之IO流

    ##Java基础之IO流IO流常用几个类的关系如下: 字节流 字节输入流FileInputStream 读取文件用...

  • Java基础——流(IO)

    流(Stream)——水流 ,可以理解为连接文件和程序的东西,Stream是从起源(source)到接收(sink...

  • Java 基础 IO流

    文章转载自http://wentao365.iteye.com/blog/1374731 字节流和字符流区别? 方...

  • java基础io流

    一、背景介绍 Java中I/O流是一种计算机用语。而I/O问题可以说是Web应用中所面临的主要问题之一,因为在这个...

  • Java基础-IO流

    流的原理在Java程序中,对于数据的输入/ 输出操作以“流” (stream) 方式进 行;J2SDK提供了各种...

  • 【Java基础】- IO流

    IO流简介 IO流 用于设备间进行数据传输的操作。 IO流可以分为字节流和字符流,也可以分为输入流和输出流。 字符...

网友评论

      本文标题:【Java基础】- IO流

      本文链接:https://www.haomeiwen.com/subject/lkldfktx.html