发布于2021-06-14 10:40 阅读(613) 评论(0) 点赞(29) 收藏(3)
技术活,该赏
关注+一键三连(点赞,评论,收藏)再看,养成好习惯
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
(从流中读取内容)
(从流中读取内容)
(从流中读取内容)
此博文的依据:hutool-5.6.5版本源码
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>
从流中读取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("关闭资源失败");
}
}
链接:待补充
从流中读取内容,读取完成后关闭流
参数名 | 描述 |
---|---|
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("关闭资源失败");
}
}
链接:待补充
从流中读取内容,读取完毕后并不关闭流
参数名 | 描述 |
---|---|
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("关闭资源失败");
}
}
链接:待补充
从流中读取内容,读到输出流中,读取完毕后并不关闭流
参数名 | 描述 |
---|---|
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("关闭资源失败");
}
}
链接:待补充
从流中读取内容,读到输出流中,读取完毕后并不关闭流
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("关闭资源失败");
}
}
链接:待补充
从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 {
}
链接:待补充
从{@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("关闭资源失败");
}
}
链接:待补充
从流中读取bytes,读取完毕后关闭流
参数名 | 描述 |
---|---|
InputStream in |
in {@link InputStream}
|
bytes
final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStream("hutool.jpg"));
Assert.assertEquals(22807, bytes.length);
链接:待补充
从流中读取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 {
}
链接:待补充
读取指定长度的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("关闭资源失败");
}
}
链接:待补充
读取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("关闭资源失败");
}
}
链接:待补充
从流中读取前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("关闭资源失败");
}
}
链接:待补充
从流中读取前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("关闭资源失败");
}
}
链接:待补充
从流中读取对象,即对象的反序列化
注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
### 支持版本及以上参数名 | 描述 |
---|---|
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);
}
链接:待补充
从流中读取对象,即对象的反序列化,读取后不关闭流
注意!!! 此方法不会检查反序列化安全,可能存在反序列化漏洞风险!!!
### 支持版本及以上参数名 | 描述 |
---|---|
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);
}
链接:待补充
从流中读取对象,即对象的反序列化,读取后不关闭流
此方法使用了{@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();
}
链接:待补充
从流中读取内容,使用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("关闭资源失败");
}
}
链接:待补充
从流中读取内容
参数名 | 描述 |
---|---|
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("关闭资源失败");
}
}
链接:待补充
从流中读取内容
参数名 | 描述 |
---|---|
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("关闭资源失败");
}
}
链接:待补充
从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("关闭资源失败");
}
}
链接:待补充
按行读取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("关闭资源失败");
}
}
}
链接:待补充
按行读取数据,针对每行的数据做处理
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("关闭资源失败");
}
}
}
链接:待补充
按行读取数据,针对每行的数据做处理<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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!