3hutool实战 IoUtil 流操作工具类(从流中读取内容)_hutool ioutil

方法明细

方法名称:cn.hutool.core.io.IoUtil.readUtf8(java.io.InputStream)
方法描述

从流中读取UTF8编码的内容

支持版本及以上

5.4.4

参数描述:
参数名描述
InputStream in
in 输入流
返回值:

内容

参考案例:
	File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			String str = IoUtil.readUtf8(input);
			System.out.println(str);
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

在这里插入图片描述

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.read(java.io.InputStream, java.lang.String)
方法描述

从流中读取内容,读取完成后关闭流

支持版本及以上
参数描述:
参数名描述
InputStream in
in 输入流
String charsetName
charsetName 字符集
返回值:

内容

参考案例:
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			String str = IoUtil.read(input,"UTF-8");
			System.out.println(str);
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.read(java.io.InputStream, java.nio.charset.Charset)
方法描述

从流中读取内容,读取完毕后并不关闭流

支持版本及以上
参数描述:
参数名描述
InputStream in
in 输入流,读取完毕后并不关闭流
Charset charset
charset 字符集
返回值:

内容

参考案例:
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			String str = IoUtil.read(input,CharsetUtil.CHARSET_UTF_8);
			System.out.println(str);
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.read(java.io.InputStream)
方法描述

从流中读取内容,读到输出流中,读取完毕后并不关闭流

支持版本及以上
参数描述:
参数名描述
InputStream in
in 输入流
返回值:

输出流

参考案例:
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//这种设计避免重新分配内存块而是分配新增的缓冲区,缓冲区不会被GC,数据也不会被拷贝到其他缓冲区。
			FastByteArrayOutputStream str = IoUtil.read(input);
			System.out.println(str.toString());
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.read(java.io.InputStream, boolean)
方法描述

从流中读取内容,读到输出流中,读取完毕后并不关闭流

支持版本及以上

5.5.3

参数描述:
参数名描述
InputStream in
in 输入流
boolean isClose
isClose 读取完毕后是否关闭流
返回值:

输出流

参考案例:
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//这种设计避免重新分配内存块而是分配新增的缓冲区,缓冲区不会被GC,数据也不会被拷贝到其他缓冲区。
			FastByteArrayOutputStream str = IoUtil.read(input,false);//读取完毕后是否关闭流
			System.out.println(str.toString());
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.read(java.io.Reader)
方法描述

从Reader中读取String,读取完毕后关闭Reader

支持版本及以上
参数描述:
参数名描述
Reader reader
reader Reader
返回值:

String

参考案例:
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		FileReader fr =  null;
		try {
			//创建流
			fr = new FileReader(src);
			//从Reader中读取String,读取完毕后关闭Reader
			String str = IoUtil.read(fr);
			System.out.println(str);
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.read(java.io.Reader, boolean)
方法描述

从{@link Reader}中读取String

支持版本及以上
参数描述:
参数名描述
Reader reader
reader {@link Reader}
boolean isClose
isClose 是否关闭{@link Reader}
返回值:

String

参考案例:
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		FileReader fr =  null;
		try {
			//创建流
			fr = new FileReader(src);
			//从Reader中读取String,读取完毕后关闭Reader
			String str = IoUtil.read(fr,false);//是否关闭Reader
			System.out.println(str);
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (fr != null) {
					fr.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readBytes(java.io.InputStream)
方法描述

从流中读取bytes,读取完毕后关闭流

支持版本及以上
参数描述:
参数名描述
InputStream in
in {@link InputStream}
返回值:

bytes

参考案例:
		final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStream("hutool.jpg"));
		Assert.assertEquals(22807, bytes.length);

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readBytes(java.io.InputStream, boolean)
方法描述

从流中读取bytes

支持版本及以上

5.0.4

参数描述:
参数名描述
InputStream in
in {@link InputStream}
boolean isClose
isClose 是否关闭输入流
返回值:

bytes

参考案例:
		//这个文件3KB
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			final byte[] bytes = IoUtil.readBytes(input,true);
			System.out.println(bytes.length);
			Assert.assertEquals(3011, bytes.length);
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readBytes(java.io.InputStream, int)
方法描述

读取指定长度的byte数组,不关闭流

支持版本及以上
参数描述:
参数名描述
InputStream in
in {@link InputStream},为null返回null
int length
length 长度,小于等于0返回空byte数组
返回值:

bytes

参考案例:
		//这个文件3KB
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//读取指定长度的byte数组,不关闭流
			final byte[] bytes = IoUtil.readBytes(input,1000);
			String string = new String(bytes);
			System.out.println(string);
			Assert.assertEquals(1000, bytes.length);
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

在这里插入图片描述

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readHex(java.io.InputStream, int, boolean)
方法描述

读取16进制字符串

支持版本及以上
参数描述:
参数名描述
InputStream in
in {@link InputStream}
int length
length 长度
boolean toLowerCase
toLowerCase true 传换成小写格式 , false 传换成大写格式
返回值:

16进制字符串

参考案例:
//这个文件3KB
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//读取指定长度的byte数组,不关闭流
			boolean toLowerCase = false;
			String readHex = IoUtil.readHex(input,1000,toLowerCase);
			System.out.println(readHex);
			System.out.println("--------------------");
			System.out.println(HexUtil.decodeHexStr(readHex));
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

在这里插入图片描述

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readHex28Upper(java.io.InputStream)
方法描述

从流中读取前28个byte并转换为16进制,字母部分使用大写

支持版本及以上
参数描述:
参数名描述
InputStream in
in {@link InputStream}
返回值:

16进制字符串

参考案例:
		//这个文件3KB
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//读取指定长度的byte数组,不关闭流
			//从流中读取前28个byte并转换为16进制,字母部分使用大写
			String readHex = IoUtil.readHex28Upper(input);
			System.out.println(readHex);
			System.out.println("--------------------");
			System.out.println(HexUtil.decodeHexStr(readHex));
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

在这里插入图片描述

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readHex28Lower(java.io.InputStream)
方法描述

从流中读取前28个byte并转换为16进制,字母部分使用小写

支持版本及以上
参数描述:
参数名描述
InputStream in
in {@link InputStream}
返回值:

16进制字符串

参考案例:
		//这个文件3KB
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//读取指定长度的byte数组,不关闭流
			//从流中读取前28个byte并转换为16进制,字母部分使用大写
			String readHex = IoUtil.readHex28Lower(input);
			System.out.println(readHex);
			System.out.println("--------------------");
			System.out.println(HexUtil.decodeHexStr(readHex));
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}

在这里插入图片描述

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readObj(java.io.InputStream)
方法描述

从流中读取对象,即对象的反序列化

注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!

支持版本及以上

参数描述:
参数名描述
InputStream in
in 输入流
返回值:

输出流 读取对象的类型

参考案例:
	Map<String,String> hashMap = new HashMap<>(16);
		for (int i = 0; i <10 ; i++) {
			hashMap.put("testKey"+i,"testValue"+i);
		}
		//对象必须实现Serializable接口
		byte[] bytes = ObjectUtil.serialize(hashMap);
		//内存读写流 不用回收关闭
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
		Map<String,String> hashMap1 = IoUtil.readObj(byteArrayInputStream);
		for (String key : hashMap1.keySet()) {
			String s = hashMap1.get(key);
			System.out.println(key + ": " + s);
		}

在这里插入图片描述

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readObj(java.io.InputStream, java.lang.Class)
方法描述

从流中读取对象,即对象的反序列化,读取后不关闭流

注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!

支持版本及以上

参数描述:
参数名描述
InputStream in
in 输入流
java.lang.Class clazz
clazz 读取对象类型
返回值:

输出流 读取对象的类型

参考案例:
		Map<String,String> hashMap = new HashMap<>(16);
		for (int i = 0; i <10 ; i++) {
			hashMap.put("testKey"+i,"testValue"+i);
		}
		//对象必须实现Serializable接口
		byte[] bytes = ObjectUtil.serialize(hashMap);
		//内存读写流 不用回收关闭
		ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
		Map<String,String> hashMap1 = IoUtil.readObj(byteArrayInputStream,Map.class);
		for (String key : hashMap1.keySet()) {
			String s = hashMap1.get(key);
			System.out.println(key + ": " + s);
		}

源码解析:
链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.readObj(cn.hutool.core.io.ValidateObjectInputStream, java.lang.Class)
方法描述

从流中读取对象,即对象的反序列化,读取后不关闭流

此方法使用了{@link ValidateObjectInputStream}中的黑白名单方式过滤类,用于避免反序列化漏洞\

参数描述:
参数名描述
ValidateObjectInputStream in
in 输入流,使用{@link ValidateObjectInputStream}中的黑白名单方式过滤类,用于避免反序列化漏洞
java.lang.Class clazz
clazz 读取对象类型
返回值:

输出流 读取对象的类型

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

nputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
Map<String,String> hashMap1 = IoUtil.readObj(byteArrayInputStream,Map.class);
for (String key : hashMap1.keySet()) {
String s = hashMap1.get(key);
System.out.println(key + ": " + s);
}


#### 源码解析:



链接:待补充


### 方法明细


#### 方法名称:cn.hutool.core.io.IoUtil.readObj(cn.hutool.core.io.ValidateObjectInputStream, java.lang.Class)


#### 方法描述


从流中读取对象,即对象的反序列化,读取后不关闭流


 此方法使用了{@link ValidateObjectInputStream}中的黑白名单方式过滤类,用于避免反序列化漏洞\


#### 参数描述:




| 参数名 | 描述 |
| --- | --- |
| ValidateObjectInputStream in | 
 in 输入流,使用{@link ValidateObjectInputStream}中的黑白名单方式过滤类,用于避免反序列化漏洞 
  |
| java.lang.Class clazz | 
 clazz 读取对象类型 
  |


#### 返回值:


输出流 读取对象的类型


[外链图片转存中...(img-aRbdZL6p-1714223778123)]
[外链图片转存中...(img-UYPYbTpb-1714223778124)]
[外链图片转存中...(img-1pJtfy2D-1714223778124)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**

  • 21
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值