程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(1)

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

发布于2021-06-14 10:40     阅读(613)     评论(0)     点赞(29)     收藏(3)


技术活,该赏
关注+一键三连(点赞,评论,收藏)再看,养成好习惯

hutool实战(带你掌握里面的各种工具)目录


用途:IO工具类(从流中读取内容)

使用场景

IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。

(从流中读取内容)

(从流中读取内容)

(从流中读取内容)

项目引用

此博文的依据:hutool-5.6.5版本源码

        <dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-core</artifactId>
			<version>5.6.5</version>
		</dependency>

方法摘要

方法描述
cn.hutool.core.io.IoUtil.readUtf8(java.io.InputStream)
从流中读取UTF8编码的内容
cn.hutool.core.io.IoUtil.read(java.io.InputStream, java.lang.String)
从流中读取内容,读取完成后关闭流
cn.hutool.core.io.IoUtil.read(java.io.InputStream, java.nio.charset.Charset)
从流中读取内容,读取完毕后并不关闭流
cn.hutool.core.io.IoUtil.read(java.io.InputStream)
从流中读取内容,读到输出流中,读取完毕后并不关闭流
cn.hutool.core.io.IoUtil.read(java.io.InputStream, boolean)
从流中读取内容,读到输出流中,读取完毕后并不关闭流
cn.hutool.core.io.IoUtil.read(java.io.Reader)
从Reader中读取String,读取完毕后关闭Reader
cn.hutool.core.io.IoUtil.read(java.io.Reader, boolean)
从{@link Reader}中读取String
cn.hutool.core.io.IoUtil.readBytes(java.io.InputStream)
从流中读取bytes,读取完毕后关闭流
cn.hutool.core.io.IoUtil.readBytes(java.io.InputStream, boolean)
从流中读取bytes
cn.hutool.core.io.IoUtil.readBytes(java.io.InputStream, int)
读取指定长度的byte数组,不关闭流
cn.hutool.core.io.IoUtil.readHex(java.io.InputStream, int, boolean)
读取16进制字符串
cn.hutool.core.io.IoUtil.readHex28Upper(java.io.InputStream)
从流中读取前28个byte并转换为16进制,字母部分使用大写
cn.hutool.core.io.IoUtil.readHex28Lower(java.io.InputStream)
从流中读取前28个byte并转换为16进制,字母部分使用小写
cn.hutool.core.io.IoUtil.readObj(java.io.InputStream)
从流中读取对象,即对象的反序列化

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

cn.hutool.core.io.IoUtil.readObj(java.io.InputStream, java.lang.Class)
从流中读取对象,即对象的反序列化,读取后不关闭流

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

cn.hutool.core.io.IoUtil.readObj(cn.hutool.core.io.ValidateObjectInputStream, java.lang.Class)
从流中读取对象,即对象的反序列化,读取后不关闭流

此方法使用了{@link ValidateObjectInputStream}中的黑白名单方式过滤类,
用于避免反序列化漏洞<br> 通过构造{@link ValidateObjectInputStream},
调用{@link ValidateObjectInputStream#accept(Class[])} 或者{@link ValidateObjectInputStream#refuse(Class[])}方法添加可以被序列化的类或者禁止序列化的类。

cn.hutool.core.io.IoUtil.readUtf8Lines(java.io.InputStream, T)
从流中读取内容,使用UTF-8编码
cn.hutool.core.io.IoUtil.readLines(java.io.InputStream, java.lang.String, T)
从流中读取内容
cn.hutool.core.io.IoUtil.readLines(java.io.InputStream, java.nio.charset.Charset, T)
从流中读取内容
cn.hutool.core.io.IoUtil.readLines(java.io.Reader, T)
从Reader中读取内容
cn.hutool.core.io.IoUtil.readUtf8Lines(java.io.InputStream, cn.hutool.core.io.LineHandler)
按行读取UTF-8编码数据,针对每行的数据做处理
cn.hutool.core.io.IoUtil.readLines(java.io.InputStream, java.nio.charset.Charset, cn.hutool.core.io.LineHandler)
按行读取数据,针对每行的数据做处理
cn.hutool.core.io.IoUtil.readLines(java.io.Reader, cn.hutool.core.io.LineHandler)
按行读取数据,针对每行的数据做处理<br> {@link Reader}自带编码定义,因此读取数据的编码跟随其编码。

方法明细

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

返回值:

输出流 读取对象的类型

参考案例:

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

在这里插入图片描述

源码解析:

链接:待补充

方法明细

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

方法描述

按行读取UTF-8编码数据,针对每行的数据做处理

支持版本及以上

3.1.1

参数描述:

参数名描述
InputStream in
in {@link InputStream}
LineHandler lineHandler
lineHandler 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方

返回值:

参考案例:

public class IoUtilLineHandler implements LineHandler {
	@Override
	public void handle(String line) {
		System.out.println("handle:"+line);
	}
}
//--------------
@Test
	public void readUtf8LinesTest1(){
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//从流中读取内容,使用UTF-8编码
			IoUtil.readUtf8Lines(input, new IoUtilLineHandler());
		} 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, cn.hutool.core.io.LineHandler)

方法描述

按行读取数据,针对每行的数据做处理

支持版本及以上

3.0.9

参数描述:

参数名描述
InputStream in
in {@link InputStream}
Charset charset
charset {@link Charset}编码
LineHandler lineHandler
lineHandler 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方

返回值:

参考案例:

public class IoUtilLineHandler implements LineHandler {
	@Override
	public void handle(String line) {
		System.out.println("handle:"+line);
	}
}
//--------------
@Test
	public void readLinesTest3(){
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		InputStream input =  null;
		try {
			//创建流
			input =  new FileInputStream(src);
			//从流中读取内容,使用UTF-8编码
			IoUtil.readLines(input, CharsetUtil.CHARSET_UTF_8, new IoUtilLineHandler());
		} 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, cn.hutool.core.io.LineHandler)

方法描述

按行读取数据,针对每行的数据做处理<br>
{@link Reader}自带编码定义,因此读取数据的编码跟随其编码。

支持版本及以上

参数描述:

参数名描述
Reader reader
reader {@link Reader}
LineHandler lineHandler
lineHandler 行处理接口,实现handle方法用于编辑一行的数据后入到指定地方

返回值:

参考案例:

public class IoUtilLineHandler implements LineHandler {
	@Override
	public void handle(String line) {
		System.out.println("handle:"+line);
	}
}
//--------------
@Test
	public void readLinesTest4(){
		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
		FileReader fr =  null;
		try {
			//创建流
			fr = new FileReader(src);
			//从流中读取内容,使用UTF-8编码
			IoUtil.readLines(fr, new IoUtilLineHandler());
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		} finally {
			try {
				//如果是空的 说明流创建失败 失败了不需要关闭
				if (fr != null) {
					fr.close();
				}
			} catch (Exception e) {
				//关闭资源失败 停止程序
				throw new RuntimeException("关闭资源失败");
			}
		}
	}

源码解析:

链接:待补充


所属网站分类: 技术文章 > 博客

作者:java战神

链接:http://www.javaheidong.com/blog/article/222709/a10324a90eb433c7403f/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

29 0
收藏该文
已收藏

评论内容:(最多支持255个字符)