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

举报
小虚竹 发表于 2021/10/19 00:01:54 2021/10/19
【摘要】 技术活,该赏 关注+一键三连(点赞,评论,收藏)再看,养成好习惯 12 hutool实战(带你掌握里面的各种工具)目录 用途:IO工具类(copy拷贝操作) 使用场景 IO工具类只是辅助流的读...
技术活,该赏
关注+一键三连(点赞,评论,收藏)再看,养成好习惯

  
 
  • 1
  • 2

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>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

方法摘要

方法 描述
cn.hutool.core.io.IoUtil.copy(java.io.Reader, java.io.Writer)
将Reader中的内容复制到Writer中 使用默认缓存大小,拷贝后不关闭Reader
cn.hutool.core.io.IoUtil.copy(java.io.Reader, java.io.Writer, int)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
cn.hutool.core.io.IoUtil.copy(java.io.Reader, java.io.Writer, int, cn.hutool.core.io.StreamProgress)
将Reader中的内容复制到Writer中,拷贝后不关闭Reader
cn.hutool.core.io.IoUtil.copy(java.io.InputStream, java.io.OutputStream)
拷贝流,使用默认Buffer大小,拷贝后不关闭流
cn.hutool.core.io.IoUtil.copy(java.io.InputStream, java.io.OutputStream, int)
拷贝流,拷贝后不关闭流
cn.hutool.core.io.IoUtil.copy(java.io.InputStream, java.io.OutputStream, int, cn.hutool.core.io.StreamProgress)
拷贝流,拷贝后不关闭流
cn.hutool.core.io.IoUtil.copy(java.io.FileInputStream, java.io.FileOutputStream)
拷贝文件流,使用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("关闭资源失败");
				}
			}
		}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

源码解析:

链接:待补充

  
 
  • 1

方法明细

方法名称: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("关闭资源失败");
				}
			}
		}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

源码解析:

链接:待补充

  
 
  • 1

方法明细

方法名称: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("关闭资源失败");
				}
			}
		}
	}
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

在这里插入图片描述

源码解析:

链接:待补充

  
 
  • 1

方法明细

方法名称: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("关闭资源失败");
				}
			}
		}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

源码解析:

链接:待补充

  
 
  • 1

方法明细

方法名称: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("关闭资源失败");
				}
			}
		}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

源码解析:

链接:待补充

  
 
  • 1

方法明细

方法名称: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("关闭资源失败");
				}
			}
		}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

在这里插入图片描述

源码解析:

链接:待补充

  
 
  • 1

方法明细

方法名称: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);
		}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

源码解析:

链接:待补充

  
 
  • 1

文章来源: xiaoxuzhu.blog.csdn.net,作者:小虚竹,版权归原作者所有,如需转载,请联系作者。

原文链接:xiaoxuzhu.blog.csdn.net/article/details/117672780

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。