分享

4hutool实战:IoUtil 流操作工具类(toStream转为流)

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

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


用途:IO工具类(toStream转为流)

使用场景

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

(String 转为流)

(文件转为流)

(byte[]转为流)

(不同类型的流互转)

项目引用

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

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

方法摘要

方法描述
String 转为流
String 转为流
String 转为UTF-8编码的字节流流
文件转为{@link FileInputStream}
byte[] 转为{@link ByteArrayInputStream}
{@link ByteArrayOutputStream}转为{@link ByteArrayInputStream}
转换为{@link BufferedInputStream}
转换为{@link BufferedInputStream}
转换为{@link BufferedOutputStream}
转换为{@link BufferedOutputStream}
转换为{@link BufferedReader}
转换为{@link BufferedReader}
转换为{@link BufferedWriter}
转换为{@link BufferedWriter}
将{@link InputStream}转换为支持mark标记的流<br> 若原流支持mark标记,则返回原流,否则使用{@link BufferedInputStream} 包装之
转换为{@link PushbackInputStream}<br> 如果传入的输入流已经是{@link PushbackInputStream},强转返回,否则新建一个
将指定{@link InputStream} 转换为{@link InputStream#available()}方法可用的流。
在Socket通信流中,服务端未返回数据情况下{@link InputStream#available()}方法始终为{@code 0}
因此,在读取前需要调用{@link InputStream#read()}读取一个字节(未返回会阻塞),一旦读取到了,{@link InputStream#available()}方法就正常了。
需要注意的是,在网络流中,是按照块来传输的,所以 {@link InputStream#available()} 读取到的并非最终长度,而是此次块的长度。
此方法返回对象的规则为:
  • FileInputStream 返回原对象,因为文件流的available方法本身可用
  • 其它InputStream 返回PushbackInputStream

方法明细

方法名称:cn.hutool.core.io.IoUtil.toStream(java.lang.String, java.lang.String)

方法描述

String 转为流

支持版本及以上

参数描述:

参数名描述
String content
content 内容
String charsetName
charsetName 编码

返回值:

字节流

参考案例:

//内存读写流 不用回收关闭
ByteArrayInputStream byteArrayInputStream = IoUtil.toStream("1hello 小虚竹\n2hello 小虚竹","UTF-8");
String str = IoUtil.read(byteArrayInputStream,"UTF-8");
System.out.println(str);

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toStream(java.lang.String, java.nio.charset.Charset)

方法描述

String 转为流

支持版本及以上

参数描述:

参数名描述
String content
content 内容
Charset charset
charset 编码

返回值:

字节流

参考案例:

//内存读写流 不用回收关闭
ByteArrayInputStream byteArrayInputStream = IoUtil.toStream("1hello 小虚竹\n2hello 小虚竹","UTF-8");
String str = IoUtil.read(byteArrayInputStream,CharsetUtil.UTF_8);
System.out.println(str);

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toUtf8Stream(java.lang.String)

方法描述

String 转为UTF-8编码的字节流流

支持版本及以上

4.5.1

参数描述:

参数名描述
String content
content 内容

返回值:

字节流

参考案例:

//内存读写流 不用回收关闭
ByteArrayInputStream byteArrayInputStream = IoUtil.toUtf8Stream("1hello 小虚竹\n2hello 小虚竹");
String str = IoUtil.read(byteArrayInputStream,CharsetUtil.UTF_8);
System.out.println(str);

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toStream(java.io.File)

方法描述

文件转为{@link FileInputStream}

支持版本及以上

参数描述:

参数名描述
File file
file 文件

返回值:

{@link FileInputStream}

参考案例:

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

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toStream(byte[])

方法描述

byte[] 转为{@link ByteArrayInputStream}

支持版本及以上

4.1.8

参数描述:

参数名描述
byte[] content
content 内容bytes

返回值:

字节流

参考案例:

String str = "1hello 小虚竹\n2hello 小虚竹";
byte[] sb = str.getBytes();
//内存读写流 不用回收关闭
ByteArrayInputStream byteArrayInputStream = IoUtil.toStream(sb);
String str1 = IoUtil.read(byteArrayInputStream,CharsetUtil.UTF_8);
System.out.println(str1);

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toStream(java.io.ByteArrayOutputStream)

方法描述

{@link ByteArrayOutputStream}转为{@link ByteArrayInputStream}

支持版本及以上

5.3.6

参数描述:

参数名描述
ByteArrayOutputStream out
out {@link ByteArrayOutputStream}

返回值:

字节流

参考案例:

try {
内存读写流 不用回收关闭
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
String str = "1hello 小虚竹\n2hello 小虚竹";
byte[] sb = str.getBytes();
byteArrayOutputStream.write(sb);
ByteArrayInputStream byteArrayInputStream = IoUtil.toStream(byteArrayOutputStream);
String str1 = IoUtil.read(byteArrayInputStream,CharsetUtil.UTF_8);
System.out.println(str1);
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toBuffered(java.io.InputStream)

方法描述

转换为{@link BufferedInputStream}

支持版本及以上

4.0.10

参数描述:

参数名描述
InputStream in
in {@link InputStream}

返回值:

{@link BufferedInputStream}

参考案例:

File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toBufferedTest1.txt") ;
FileInputStream fileInputStream =  null;
BufferedInputStream bufferedInputStream = null;
try {
fileInputStream = new FileInputStream(src);
bufferedInputStream = IoUtil.toBuffered(fileInputStream);
String str = IoUtil.read(bufferedInputStream,"UTF-8");
System.out.println(str);
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(fileInputStream);
IoUtil.close(bufferedInputStream);
}

在这里插入图片描述

源码解析:

链接:待补充

方法明细

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

方法描述

转换为{@link BufferedInputStream}

支持版本及以上

5.6.1

参数描述:

参数名描述
InputStream in
in {@link InputStream}
int bufferSize
bufferSize buffer size

返回值:

{@link BufferedInputStream}

参考案例:

File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toBufferedTest1.txt") ;
FileInputStream fileInputStream =  null;
BufferedInputStream bufferedInputStream = null;
try {
fileInputStream = new FileInputStream(src);
bufferedInputStream = IoUtil.toBuffered(fileInputStream,8192);
String str = IoUtil.read(bufferedInputStream,"UTF-8");
System.out.println(str);
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(fileInputStream);
IoUtil.close(bufferedInputStream);
}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toBuffered(java.io.OutputStream)

方法描述

转换为{@link BufferedOutputStream}

支持版本及以上

4.0.10

参数描述:

参数名描述
OutputStream out
out {@link OutputStream}

返回值:

{@link BufferedOutputStream}

参考案例:

File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toBufferedTest3.txt") ;
OutputStream outputStream =  null;
BufferedOutputStream bufferedOutputStream = null;
try {
//创建流
outputStream = new FileOutputStream(src);
bufferedOutputStream = IoUtil.toBuffered(outputStream);
String str = "toBufferedTest3内容1 \ntoBufferedTest3内容2";
byte[] sb = str.getBytes();
bufferedOutputStream.write(sb);
bufferedOutputStream.flush();
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(outputStream);
IoUtil.close(bufferedOutputStream);
}

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toBuffered(java.io.OutputStream, int)

方法描述

转换为{@link BufferedOutputStream}

支持版本及以上

5.6.1

参数描述:

参数名描述
OutputStream out
out {@link OutputStream}
int bufferSize
bufferSize buffer size

返回值:

{@link BufferedOutputStream}

参考案例:

File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toBufferedTest3.txt") ;
OutputStream outputStream =  null;
BufferedOutputStream bufferedOutputStream = null;
try {
//创建流
outputStream = new FileOutputStream(src);
bufferedOutputStream = IoUtil.toBuffered(outputStream,8192);
String str = "toBufferedTest3内容1 \ntoBufferedTest3内容2";
byte[] sb = str.getBytes();
bufferedOutputStream.write(sb);
bufferedOutputStream.flush();
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(outputStream);
IoUtil.close(bufferedOutputStream);
}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toBuffered(java.io.Reader)

方法描述

转换为{@link BufferedReader}

支持版本及以上

5.6.1

参数描述:

参数名描述
Reader reader
reader {@link Reader}

返回值:

{@link BufferedReader}

参考案例:

Reader reader =  null;
BufferedReader bufferedReader = null;
try {
reader = new StringReader("1hello 小虚竹\n2hello 小虚竹");
bufferedReader = IoUtil.toBuffered(reader);
//读第一行数据
String str = "";
while (str != null){
str = bufferedReader.readLine();
if(str !=null){
System.out.println(str);
}
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(reader);
IoUtil.close(bufferedReader);
}

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toBuffered(java.io.Reader, int)

方法描述

转换为{@link BufferedReader}

支持版本及以上

5.6.1

参数描述:

参数名描述
Reader reader
reader {@link Reader}
int bufferSize
bufferSize buffer size

返回值:

{@link BufferedReader}

参考案例:

Reader reader =  null;
BufferedReader bufferedReader = null;
try {
reader = new StringReader("1hello 小虚竹\n2hello 小虚竹");
bufferedReader = IoUtil.toBuffered(reader,8192);
//读第一行数据
String str = "";
while (str != null){
str = bufferedReader.readLine();
if(str !=null){
System.out.println(str);
}
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(reader);
IoUtil.close(bufferedReader);
}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toBuffered(java.io.Writer)

方法描述

转换为{@link BufferedWriter}

支持版本及以上

5.6.1

参数描述:

参数名描述
Writer writer
writer {@link Writer}

返回值:

{@link BufferedWriter}

参考案例:

File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toBufferedTest7.txt") ;
FileWriter fw = null;
BufferedWriter bufferedWriter = null;
try {
//创建流
fw = new FileWriter(dest);
bufferedWriter = IoUtil.toBuffered(fw);
String str = "toBufferedTest7 \ntoBufferedTest7";
bufferedWriter.write(str);
bufferedWriter.flush();
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(fw);
IoUtil.close(bufferedWriter);
}

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toBuffered(java.io.Writer, int)

方法描述

转换为{@link BufferedWriter}

支持版本及以上

5.6.1

参数描述:

参数名描述
Writer writer
writer {@link Writer}
int bufferSize
bufferSize buffer size

返回值:

{@link BufferedWriter}

参考案例:


File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toBufferedTest7.txt") ;
FileWriter fw = null;
BufferedWriter bufferedWriter = null;
try {
//创建流
fw = new FileWriter(dest);
bufferedWriter = IoUtil.toBuffered(fw,8192);
String str = "toBufferedTest7 \ntoBufferedTest7";
bufferedWriter.write(str);
bufferedWriter.flush();
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(fw);
IoUtil.close(bufferedWriter);
}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toMarkSupportStream(java.io.InputStream)

方法描述

将{@link InputStream}转换为支持mark标记的流<br>
若原流支持mark标记,则返回原流,否则使用{@link BufferedInputStream} 包装之

支持版本及以上

4.0.9

参数描述:

参数名描述
InputStream in
in 流

返回值:

{@link InputStream}

参考案例:

File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toMarkSupportStreamTest1.txt") ;
FileInputStream input = null;
InputStream inputStream = null;
try {
//创建流
input =  new FileInputStream(src);
inputStream = IoUtil.toMarkSupportStream(input);
inputStream.mark(10);
int c = 0;
int index = 0;
//避免陷入死循环
int breakRead = 0;
StringBuilder stringBuilder = new StringBuilder();
while ((c = inputStream.read()) != -1 && breakRead<2){
index++;
stringBuilder.append((char) c);
if(index>7){
index=0;
breakRead++;
//支持mark标记,则返回原流
inputStream.reset();
}
}
System.out.println(stringBuilder.toString());
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(inputStream);
IoUtil.close(input);
}

在这里插入图片描述

在这里插入图片描述

源码解析:

链接:待补充

方法明细

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

方法描述

转换为{@link PushbackInputStream}<br>
如果传入的输入流已经是{@link PushbackInputStream},强转返回,否则新建一个

支持版本及以上

3.1.0

参数描述:

参数名描述
InputStream in
in {@link InputStream}
int pushBackSize
pushBackSize 推后的byte数

返回值:

{@link PushbackInputStream}

参考案例:

File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toPushbackStreamTest.txt") ;
FileInputStream input = null;
PushbackInputStream pushbackInputStream = null;
try {
//创建流
input =  new FileInputStream(src);
//在输入流中某个不需要的内容,可以通过pushbackInputStream(回退输入流)处理掉
pushbackInputStream = IoUtil.toPushbackStream(input,1);
System.out.print("读取之后的数据为:") ;
int temp = 0 ;
while((temp=pushbackInputStream.read())!=-1) {    // 读取内容
if (temp == 'P') {    // 判断是否读取到了“.”
pushbackInputStream.unread(temp);    // 放回到缓冲区之中
temp = pushbackInputStream.read();    // 再读一遍
System.out.print("(退回" + (char) temp + ")");
} else {
System.out.print((char) temp);    // 输出内容
}
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(pushbackInputStream);
IoUtil.close(input);
}

在这里插入图片描述

在这里插入图片描述

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.IoUtil.toAvailableStream(java.io.InputStream)

方法描述

将指定{@link InputStream} 转换为{@link InputStream#available()}方法可用的流。

在Socket通信流中,服务端未返回数据情况下{@link InputStream#available()}方法始终为{@code 0}

因此,在读取前需要调用{@link InputStream#read()}读取一个字节(未返回会阻塞),一旦读取到了,{@link InputStream#available()}方法就正常了。

需要注意的是,在网络流中,是按照块来传输的,所以 {@link InputStream#available()} 读取到的并非最终长度,而是此次块的长度。

此方法返回对象的规则为:

  • FileInputStream 返回原对象,因为文件流的available方法本身可用
  • 其它InputStream 返回PushbackInputStream

支持版本及以上

5.5.3

参数描述:

参数名描述
InputStream in
in 被转换的流

返回值:

转换后的流,可能为{@link PushbackInputStream}

参考案例:

File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toAvailableStreamTest.txt") ;
FileInputStream fileInputStream = null;
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
//创建流
fileInputStream =  new FileInputStream(src);
//返回原对象 FileInputStream
inputStream = IoUtil.toAvailableStream(fileInputStream);

int c = 0;
StringBuilder stringBuilder = new StringBuilder();
while ((c = inputStream.read())!= -1){
stringBuilder.append((char) c);
}
System.out.println(stringBuilder.toString());
System.out.print("----------------------------") ;

fileInputStream = new FileInputStream(src);
//其它InputStream 返回PushbackInputStream
bufferedInputStream = IoUtil.toBuffered(fileInputStream);
inputStream = IoUtil.toAvailableStream(bufferedInputStream);
int temp = 0;
while ((temp=((PushbackInputStream)inputStream).read())!=-1){
if (temp == 'A') {    // 判断是否读取到了“.”
((PushbackInputStream)inputStream).unread(temp);    // 放回到缓冲区之中
temp = inputStream.read();    // 再读一遍
System.out.print("(退回" + (char) temp + ")");
} else {
System.out.print((char) temp);    // 输出内容
}
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
IoUtil.close(fileInputStream);
IoUtil.close(inputStream);
IoUtil.close(bufferedInputStream);

}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

源码解析:

链接:待补充

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多