用途:IO工具类(流的常用操作)
使用场景
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
包含但不限于:flush,close,对比两个流内容,计算流的校验码和返回行遍历器等。
项目引用
此博文的依据:hutool-5.6.5版本源码
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core</artifactId> <version>5.6.5</version> </dependency>
返回值:
参考案例:
//传统的写法 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); }
返回值:
参考案例:
//传统的写法 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); }
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--"); }
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--"); }
方法明细
方法名称:cn.hutool.core.io.IoUtil.contentEquals(java.io.InputStream, java.io.InputStream)
方法描述
对比两个流内容是否相同<br>
内部会转换流为 {@link BufferedInputStream}
支持版本及以上
4.0.6
参数描述:
参考案例2:
参考案例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--"); }