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

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

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

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

			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("关闭资源失败");
		}
	}

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607195859514.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaV9ob25nX2ZlaV9oZWk=,size_16,color_FFFFFF,t_70#pic_center)


#### 源码解析:



链接:待补充


### 方法明细


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


![在这里插入图片描述](https://img-blog.csdnimg.cn/2021060719591658.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaV9ob25nX2ZlaV9oZWk=,size_16,color_FFFFFF,t_70#pic_center)


#### 源码解析:



链接:待补充


### 方法明细


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

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607195937965.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaV9ob25nX2ZlaV9oZWk=,size_16,color_FFFFFF,t_70#pic_center)


#### 源码解析:



链接:待补充


### 方法明细


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

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607195955750.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaV9ob25nX2ZlaV9oZWk=,size_16,color_FFFFFF,t_70#pic_center)


#### 源码解析:



链接:待补充


### 方法明细


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

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607200010964.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaV9ob25nX2ZlaV9oZWk=,size_16,color_FFFFFF,t_70#pic_center)


#### 源码解析:



链接:待补充


### 方法明细


#### 方法名称: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 读取对象类型 
  |


#### 返回值:


输出流 读取对象的类型


#### 参考案例:



	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);
	//内存读写流 不用回收关闭
	//带有类验证的对象流,用于避免反序列化漏洞<br>
	ValidateObjectInputStream validateObjectInputStream = null;
	try {
		validateObjectInputStream = new ValidateObjectInputStream(new ByteArrayInputStream(bytes), Map.class);
		Map<String,String> hashMap1 = IoUtil.readObj(validateObjectInputStream,Map.class);
		for (String key : hashMap1.keySet()) {
			String s = hashMap1.get(key);
			System.out.println(key + ": " + s);
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

#### 源码解析:



链接:待补充


### 方法明细


#### 方法名称:cn.hutool.core.io.IoUtil.readUtf8Lines(java.io.InputStream, T)


#### 方法描述


从流中读取内容,使用UTF-8编码


#### 支持版本及以上


#### 参数描述:




| 参数名 | 描述 |
| --- | --- |
| InputStream in | 
 in 输入流 
  |
| java.util.Collection collection | 
 collection 返回集合 
  |


#### 返回值:


内容 集合类型


#### 参考案例:



	File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
	InputStream input =  null;
	try {
		//创建流
		input =  new FileInputStream(src);
		//从流中读取内容,使用UTF-8编码
		ArrayList<String> readUtf8Lines = IoUtil.readUtf8Lines(input, new ArrayList<>());
		for(String readUtf8Line :readUtf8Lines){
			System.out.println(readUtf8Line);
		}
	} catch (IOException e) {
		//抛出一个运行时异常(直接停止掉程序)
		throw new RuntimeException("运行时异常",e);
	} finally {
		try {
			//如果是空的 说明流创建失败 失败了不需要关闭
			if (input != null) {
				input.close();
			}
		} catch (Exception e) {
			//关闭资源失败 停止程序
			throw new RuntimeException("关闭资源失败");
		}
	}

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607200030705.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NoaV9ob25nX2ZlaV9oZWk=,size_16,color_FFFFFF,t_70#pic_center)


#### 源码解析:



链接:待补充


### 方法明细


#### 方法名称:cn.hutool.core.io.IoUtil.readLines(java.io.InputStream, java.lang.String, T)


#### 方法描述


从流中读取内容


#### 支持版本及以上


#### 参数描述:




| 参数名 | 描述 |
| --- | --- |
| InputStream in | 
 in 输入流 
  |
| String charsetName | 
 charsetName 字符集 
  |
| java.util.Collection collection | 
 collection 返回集合 
  |


#### 返回值:


内容 集合类型


#### 参考案例:



	File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
	InputStream input =  null;
	try {
		//创建流
		input =  new FileInputStream(src);
		//从流中读取内容,使用UTF-8编码
		ArrayList<String> readLines = IoUtil.readLines(input,"UTF-8", new ArrayList<>());
		for(String readLine :readLines){
			System.out.println(readLine);
		}
	} 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.readLines(java.io.InputStream, java.nio.charset.Charset, T)


#### 方法描述


从流中读取内容


#### 支持版本及以上


#### 参数描述:




| 参数名 | 描述 |
| --- | --- |
| InputStream in | 
 in 输入流 
  |
| Charset charset | 
 charset 字符集 
  |
| java.util.Collection collection | 
 collection 返回集合 
  |


#### 返回值:


内容 集合类型


#### 参考案例:



File src = new File(“C:\Users\Administrator\Desktop\xuzhu/copyTest1.txt”) ;
InputStream input = null;
try {
//创建流
input = new FileInputStream(src);
//从流中读取内容,使用UTF-8编码
ArrayList readLines = IoUtil.readLines(input,CharsetUtil.CHARSET_UTF_8, new ArrayList<>());
for(String readLine :readLines){
System.out.println(readLine);
}
} 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.readLines(java.io.Reader, T)


#### 方法描述


从Reader中读取内容


#### 支持版本及以上


#### 参数描述:




| 参数名 | 描述 |
| --- | --- |
| Reader reader | 
 reader {@link Reader} 
  |
| java.util.Collection collection | 
 collection 返回集合 
  |


#### 返回值:


内容 集合类型


#### 参考案例:



File src = new File(“C:\Users\Administrator\Desktop\xuzhu/copyTest1.txt”) ;
FileReader fr = null;
try {
//创建流
fr = new FileReader(src);
//从流中读取内容,使用UTF-8编码
ArrayList readLines = IoUtil.readLines(fr, new ArrayList<>());
for(String readLine :readLines){
System.out.println(readLine);
}
} 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.readUtf8Lines(java.io.InputStream, cn.hutool.core.io.LineHandler)




![img](https://img-blog.csdnimg.cn/img_convert/906976457f67279df348e9063f2c6e0b.png)
![img](https://img-blog.csdnimg.cn/img_convert/36858724398c99446d2952e4d646e47f.png)
![img](https://img-blog.csdnimg.cn/img_convert/2349312adf3eb02a31d920ba11c0e342.png)

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

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

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

``
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		FileReader fr =  null;
		try {
			//创建流
			fr = new FileReader(src);
			//从流中读取内容,使用UTF-8编码
			ArrayList<String> readLines = IoUtil.readLines(fr, new ArrayList<>());
			for(String readLine :readLines){
				System.out.println(readLine);
			}
		} 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.readUtf8Lines(java.io.InputStream, cn.hutool.core.io.LineHandler)

[外链图片转存中…(img-zZj91sXg-1715666527991)]
[外链图片转存中…(img-xQ2oXjMq-1715666527991)]
[外链图片转存中…(img-G4Fi2KgW-1715666527992)]

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

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

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

  • 27
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值