分享

1hutool实战:IoUtil 流操作工具类(copy拷贝操作)

 小虚竹 2021-11-30

hutool实战(带你掌握里面的各种工具)目录


用途:IO工具类(copy拷贝操作)

使用场景

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

(copy拷贝操作)

(copy拷贝操作)

(copy拷贝操作)

项目引用

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

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

方法摘要

方法描述

将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader

将Reader中的内容复制到Writer中,拷贝后不关闭Reader

将Reader中的内容复制到Writer中,拷贝后不关闭Reader

拷贝流,使用默认Buffer大小,拷贝后不关闭流

拷贝流,拷贝后不关闭流

拷贝流,拷贝后不关闭流

拷贝文件流,使用NIO

方法明细

方法名称:cn.hutool.core.io.IoUtil.copy(java.io.Reader, java.io.Writer)

方法描述

将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader

支持版本及以上

参数描述:

参数名描述
Reader reader
reader Reader
Writer writer
writer Writer

返回值:

拷贝的字节数

参考案例:

//事先创建源文件,目标文件可以不用创建File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;FileWriter fw = null;FileReader fr = null;try {//创建流fw = new FileWriter(dest);fr = new FileReader(src);IoUtil.copy(fr,fw);} catch (IOException e) {//抛出一个运行时异常(直接停止掉程序)throw new RuntimeException("运行时异常",e);} finally {try {//如果是空的 说明流创建失败 失败了不需要关闭if (fw != null) {fw.close();}} catch (Exception e) {//关闭资源失败 停止程序throw new RuntimeException("关闭资源失败");}finally {try {if (fr != null) {fr.close();}} catch (Exception e) {throw new RuntimeException("关闭资源失败");}}}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.copy(java.io.Reader, java.io.Writer, int)

方法描述

将Reader中的内容复制到Writer中,拷贝后不关闭Reader

支持版本及以上

参数描述:

参数名描述
Reader reader
reader Reader
Writer writer
writer Writer
int bufferSize
bufferSize 缓存大小

返回值:

传输的byte数

参考案例:

//事先创建源文件,目标文件可以不用创建File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;FileWriter fw = null;FileReader fr = null;try {//创建流fw = new FileWriter(dest);fr = new FileReader(src);IoUtil.copy(fr,fw,NioUtil.DEFAULT_MIDDLE_BUFFER_SIZE);} catch (IOException e) {//抛出一个运行时异常(直接停止掉程序)throw new RuntimeException("运行时异常",e);} finally {try {//如果是空的 说明流创建失败 失败了不需要关闭if (fw != null) {fw.close();}} catch (Exception e) {//关闭资源失败 停止程序throw new RuntimeException("关闭资源失败");}finally {try {if (fr != null) {fr.close();}} catch (Exception e) {throw new RuntimeException("关闭资源失败");}}}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.copy(java.io.Reader, java.io.Writer, int, cn.hutool.core.io.StreamProgress)

方法描述

将Reader中的内容复制到Writer中,拷贝后不关闭Reader

支持版本及以上

参数描述:

参数名描述
Reader reader
reader Reader
Writer writer
writer Writer
int bufferSize
bufferSize 缓存大小
StreamProgress streamProgress
streamProgress 进度处理器

返回值:

传输的byte数

参考案例:

public class StreamProgressObj implements StreamProgress {@Overridepublic void start() {System.out.println("copy操作进度开始");}@Overridepublic void progress(long progressSize) {System.out.println("当前copy操作进度:"+progressSize);}@Overridepublic void finish() {System.out.println("copy操作进度结束");}}public class IoUtilTest {@Testpublic void copyTest3(){//事先创建源文件,目标文件可以不用创建File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;FileWriter fw = null;FileReader fr = null;try {//创建流fw = new FileWriter(dest);fr = new FileReader(src);StreamProgressObj streamProgressObj = new StreamProgressObj();IoUtil.copy(fr,fw,1024,streamProgressObj);} catch (IOException e) {//抛出一个运行时异常(直接停止掉程序)throw new RuntimeException("运行时异常",e);} finally {try {//如果是空的 说明流创建失败 失败了不需要关闭if (fw != null) {fw.close();}} catch (Exception e) {//关闭资源失败 停止程序throw new RuntimeException("关闭资源失败");}finally {try {if (fr != null) {fr.close();}} catch (Exception e) {throw new RuntimeException("关闭资源失败");}}}}}

在这里插入图片描述

源码解析:

链接:待补充

方法明细

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

方法描述

拷贝流,使用默认Buffer大小,拷贝后不关闭流

支持版本及以上

参数描述:

参数名描述
InputStream in
in 输入流
OutputStream out
out 输出流

返回值:

传输的byte数

参考案例:

//事先创建源文件,目标文件可以不用创建File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;InputStream input =  null;OutputStream outputStream = null;try {//创建流input =  new FileInputStream(src);outputStream = new FileOutputStream(dest);IoUtil.copy(input,outputStream);} catch (IOException e) {//抛出一个运行时异常(直接停止掉程序)throw new RuntimeException("运行时异常",e);} finally {try {//如果是空的 说明流创建失败 失败了不需要关闭if (input != null) {input.close();}} catch (Exception e) {//关闭资源失败 停止程序throw new RuntimeException("关闭资源失败");}finally {try {if (outputStream != null) {outputStream.close();}} catch (Exception e) {throw new RuntimeException("关闭资源失败");}}}

源码解析:

链接:待补充

方法明细

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

方法描述

拷贝流,拷贝后不关闭流

支持版本及以上

参数描述:

参数名描述
InputStream in
in 输入流
OutputStream out
out 输出流
int bufferSize
bufferSize 缓存大小

返回值:

传输的byte数

参考案例:

//事先创建源文件,目标文件可以不用创建File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;InputStream input =  null;OutputStream outputStream = null;try {//创建流input =  new FileInputStream(src);outputStream = new FileOutputStream(dest);IoUtil.copy(input,outputStream,NioUtil.DEFAULT_BUFFER_SIZE);} catch (IOException e) {//抛出一个运行时异常(直接停止掉程序)throw new RuntimeException("运行时异常",e);} finally {try {//如果是空的 说明流创建失败 失败了不需要关闭if (input != null) {input.close();}} catch (Exception e) {//关闭资源失败 停止程序throw new RuntimeException("关闭资源失败");}finally {try {if (outputStream != null) {outputStream.close();}} catch (Exception e) {throw new RuntimeException("关闭资源失败");}}}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.copy(java.io.InputStream, java.io.OutputStream, int, cn.hutool.core.io.StreamProgress)

方法描述

拷贝流,拷贝后不关闭流

支持版本及以上

参数描述:

参数名描述
InputStream in
in 输入流
OutputStream out
out 输出流
int bufferSize
bufferSize 缓存大小
StreamProgress streamProgress
streamProgress 进度条

返回值:

传输的byte数

参考案例:

//事先创建源文件,目标文件可以不用创建File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;InputStream input =  null;OutputStream outputStream = null;try {//创建流input =  new FileInputStream(src);outputStream = new FileOutputStream(dest);StreamProgressObj streamProgressObj = new StreamProgressObj();IoUtil.copy(input,outputStream,1024,streamProgressObj);} catch (IOException e) {//抛出一个运行时异常(直接停止掉程序)throw new RuntimeException("运行时异常",e);} finally {try {//如果是空的 说明流创建失败 失败了不需要关闭if (input != null) {input.close();}} catch (Exception e) {//关闭资源失败 停止程序throw new RuntimeException("关闭资源失败");}finally {try {if (outputStream != null) {outputStream.close();}} catch (Exception e) {throw new RuntimeException("关闭资源失败");}}}

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.copy(java.io.FileInputStream, java.io.FileOutputStream)

方法描述

拷贝文件流,使用NIO

支持版本及以上

参数描述:

参数名描述
FileInputStream in
in 输入
FileOutputStream out
out 输出

返回值:

拷贝的字节数

参考案例:

//拷贝文件流,使用NIO 使用后会程序会关掉流//事先创建源文件,目标文件可以不用创建File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;FileInputStream input =  null;FileOutputStream outputStream = null;try {//创建流input =  new FileInputStream(src);outputStream = new FileOutputStream(dest);IoUtil.copy(input,outputStream);} catch (IOException e) {//抛出一个运行时异常(直接停止掉程序)throw new RuntimeException("运行时异常",e);}

源码解析:

链接:待补充

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多