方法名称: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 {
@Override
public void start() {
System.out.println(“copy操作进度开始”);
}
@Override
public void progress(long progressSize) {
System.out.println(“当前copy操作进度:”+progressSize);
}
@Override
public void finish() {
System.out.println(“copy操作进度结束”);
}
}
public class IoUtilTest {
@Test
public 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(“关闭资源失败”)