hutool实战(带你掌握里面的各种工具)目录
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 |
将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("关闭资源失败");}}}
链接:待补充
将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("关闭资源失败");}}}
链接:待补充
将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("关闭资源失败");}}}}}
链接:待补充
拷贝流,使用默认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("关闭资源失败");}}}
链接:待补充
拷贝流,拷贝后不关闭流
参数名 | 描述 |
---|---|
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("关闭资源失败");}}}
链接:待补充
拷贝流,拷贝后不关闭流
参数名 | 描述 |
---|---|
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("关闭资源失败");}}}
链接:待补充
拷贝文件流,使用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);}
链接:待补充
联系客服