6hutool实战:IoUtil 流操作工具类(流的常用操作)

简介: 6hutool实战:IoUtil 流操作工具类(流的常用操作)

image.png

用途:IO工具类(流的常用操作)

使用场景

IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。

包含但不限于:flush,close,对比两个流内容,计算流的校验码和返回行遍历器等。


项目引用

此博文的依据:hutool-5.6.5版本源码

        <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-core</artifactId>
      <version>5.6.5</version>
    </dependency>

image.png

返回值:

参考案例:

//传统的写法
    File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
    FileOutputStream outputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    try {
      //创建流
      outputStream = new FileOutputStream(dest);
      //新new OutputStreamWriter对象,记得关闭回收
      outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
      String content = "1hello 小虚竹\n2hello 小虚竹";
      int c;
      for (int i = 0; i < content.length(); i++) {
        c = content.charAt(i);
        outputStreamWriter.write((char) c);
      }
      outputStreamWriter.flush();
    } catch (IOException e) {
      //抛出一个运行时异常(直接停止掉程序)
      throw new RuntimeException("运行时异常",e);
    } finally {
      try {
        //如果是空的 说明流创建失败 失败了不需要关闭
        if (outputStream != null) {
          outputStream.close();
        }
      } catch (Exception e) {
        //关闭资源失败 停止程序
        throw new RuntimeException("关闭资源失败");
      }finally {
        try {
          if (outputStreamWriter != null) {
            outputStreamWriter.close();
          }
        } catch (Exception e) {
          throw new RuntimeException("关闭资源失败");
        }
      }
    }
    //使用IoUtil.flush的写法
        File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
    FileOutputStream outputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    try {
      //创建流
      outputStream = new FileOutputStream(dest);
      //新new OutputStreamWriter对象,记得关闭回收
      outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
      String content = "1hello 小虚竹\n2hello 小虚竹";
      int c;
      for (int i = 0; i < content.length(); i++) {
        c = content.charAt(i);
        outputStreamWriter.write((char) c);
      }
      IoUtil.flush(outputStreamWriter);
    } catch (IOException e) {
      //抛出一个运行时异常(直接停止掉程序)
      throw new RuntimeException("运行时异常",e);
    } finally {
      IoUtil.close(outputStream);
      IoUtil.close(outputStreamWriter);
    }

image.png

返回值:

参考案例:

//传统的写法
    File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
    FileOutputStream outputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    try {
      //创建流
      outputStream = new FileOutputStream(dest);
      //新new OutputStreamWriter对象,记得关闭回收
      outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
      String content = "1hello 小虚竹\n2hello 小虚竹";
      int c;
      for (int i = 0; i < content.length(); i++) {
        c = content.charAt(i);
        outputStreamWriter.write((char) c);
      }
      outputStreamWriter.flush();
    } catch (IOException e) {
      //抛出一个运行时异常(直接停止掉程序)
      throw new RuntimeException("运行时异常",e);
    } finally {
      try {
        //如果是空的 说明流创建失败 失败了不需要关闭
        if (outputStream != null) {
          outputStream.close();
        }
      } catch (Exception e) {
        //关闭资源失败 停止程序
        throw new RuntimeException("关闭资源失败");
      }finally {
        try {
          if (outputStreamWriter != null) {
            outputStreamWriter.close();
          }
        } catch (Exception e) {
          throw new RuntimeException("关闭资源失败");
        }
      }
    }
    //使用IoUtil.close的写法
        File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
    FileOutputStream outputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    try {
      //创建流
      outputStream = new FileOutputStream(dest);
      //新new OutputStreamWriter对象,记得关闭回收
      outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
      String content = "1hello 小虚竹\n2hello 小虚竹";
      int c;
      for (int i = 0; i < content.length(); i++) {
        c = content.charAt(i);
        outputStreamWriter.write((char) c);
      }
      IoUtil.flush(outputStreamWriter);
    } catch (IOException e) {
      //抛出一个运行时异常(直接停止掉程序)
      throw new RuntimeException("运行时异常",e);
    } finally {
      IoUtil.close(outputStream);
      IoUtil.close(outputStreamWriter);
    }

image.png

    public class AutoCloseableObject implements AutoCloseable{
  @Override
  public void close() throws Exception {
    System.out.println("--close AutoCloseableObject--");
  }
  public void demo(){
    System.out.println("这是AutoCloseableObject对象里的demo方法");
    //1.7之后,只要实现了AutoCloseable接口
    try (FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt")) {
      System.out.println("--fileInputStream--");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
    //----------------------------------
    //先了解下AutoCloseable的工作原理
    //1、AutoCloseable jdk1.7+才支持
    //2、try方法可以管理多个资源,用;号隔开
    //3、管理的资源一定要实现AutoCloseable接口
    try(AutoCloseableObject object = new AutoCloseableObject())  {
      System.out.println("--执行closeIfPosibleTest方法--");
      System.out.println("--demo--");
      object.demo();
    }catch (Exception e){
      System.out.println("--exception--");
    }finally {
      System.out.println("--finally--");
    }

image.png

  try (FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt")) {
      System.out.println("--fileInputStream--");
      IoUtil.closeIfPosible(fileInputStream);
      System.out.println(fileInputStream);
    }catch (Exception e){
      System.out.println("--exception--");
    }finally {
      System.out.println("--finally--");
    }

image.png方法明细

方法名称:cn.hutool.core.io.IoUtil.contentEquals(java.io.InputStream, java.io.InputStream)

方法描述

对比两个流内容是否相同<br>

内部会转换流为 {@link BufferedInputStream}


支持版本及以上

4.0.6


参数描述:

image.png

参考案例2:

image.pngimage.pngimage.png

参考案例2:

    //对比两个流内容是否相同,忽略EOL字符 像\n,\r 就是EOL字符
    File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
    File flushFile = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest2.txt") ;
    try (FileReader srcFileFileReader = new FileReader(src);
       FileReader flushFileFileReader = new FileReader(flushFile)) {
      Assert.assertEquals(Boolean.TRUE, IoUtil.contentEqualsIgnoreEOL(srcFileFileReader,flushFileFileReader));
    }catch (Exception e){
      System.out.println("--exception--");
    }finally {
      System.out.println("--finally--");
    }

image.pngimage.pngimage.pngimage.pngimage.png

目录
相关文章
|
6月前
|
Java
【Java】Stream流是什么,如何使用Stream流?
【Java】Stream流是什么,如何使用Stream流?
82 0
|
6月前
|
Java
Java8 Stream流 使用笔记
Java8 Stream流 使用笔记
59 1
|
6月前
|
安全 Java 大数据
深入 Java Stream:高级流操作和技巧
深入 Java Stream:高级流操作和技巧
408 0
java202303java学习笔记第三十三天stream流思想和获取stream流4
java202303java学习笔记第三十三天stream流思想和获取stream流4
46 0
java202303java学习笔记第三十六天字符输入流的底层原理解析2
java202303java学习笔记第三十六天字符输入流的底层原理解析2
62 0
java202303java学习笔记第三十五天字符输入流的底层原理解析1
java202303java学习笔记第三十五天字符输入流的底层原理解析1
69 0
java202303java学习笔记第三十六天字符输入流的底层原理解析3
java202303java学习笔记第三十六天字符输入流的底层原理解析3
129 0
java202303java学习笔记第三十三天stream流思想和获取stream流2
java202303java学习笔记第三十三天stream流思想和获取stream流2
44 0
java202303java学习笔记第三十三天stream流思想和获取stream流1
java202303java学习笔记第三十三天stream流思想和获取stream流1
50 0
|
算法 Java
还有人不知道 Java 8 Stream流底层原理?
还有人不知道 Java 8 Stream流底层原理?