文章23 | 阅读 30927 | 点赞0
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
包含但不限于:flush,close,对比两个流内容,计算流的校验码和返回行遍历器等。
此博文的依据:hutool-5.6.5版本源码
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>
方法 | 描述 |
---|---|
cn.hutool.core.io.IoUtil.flush(java.io.Flushable) | 从缓存中刷出数据 |
cn.hutool.core.io.IoUtil.close(java.io.Closeable) | 关闭<br> 关闭失败不会抛出异常 |
cn.hutool.core.io.IoUtil.closeIfPosible(java.lang.Object) | 尝试关闭指定对象<br> 判断对象如果实现了{@link AutoCloseable},则调用之 |
cn.hutool.core.io.IoUtil.contentEquals(java.io.InputStream, java.io.InputStream) | 对比两个流内容是否相同<br> 内部会转换流为 {@link BufferedInputStream} |
cn.hutool.core.io.IoUtil.contentEquals(java.io.Reader, java.io.Reader) | 对比两个Reader的内容是否一致<br> 内部会转换流为 {@link BufferedInputStream} |
cn.hutool.core.io.IoUtil.contentEqualsIgnoreEOL(java.io.Reader, java.io.Reader) | 对比两个流内容是否相同,忽略EOL字符<br> 内部会转换流为 {@link BufferedInputStream} |
cn.hutool.core.io.IoUtil.checksumCRC32(java.io.InputStream) | 计算流CRC32校验码,计算后关闭流 |
cn.hutool.core.io.IoUtil.checksum(java.io.InputStream, java.util.zip.Checksum) | 计算流的校验码,计算后关闭流 |
cn.hutool.core.io.IoUtil.checksumValue(java.io.InputStream, java.util.zip.Checksum) | 计算流的校验码,计算后关闭流 |
cn.hutool.core.io.IoUtil.lineIter(java.io.Reader) | 返回行遍历器 |
LineIterator it = null; try { it = IoUtil.lineIter(reader); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
|
| cn.hutool.core.io.IoUtil.lineIter(java.io.InputStream, java.nio.charset.Charset) | 返回行遍历器
LineIterator it = null; try { it = IoUtil.lineIter(in, CharsetUtil.CHARSET_UTF_8); while (it.hasNext()) { String line = it.nextLine(); // do something with line } } finally { it.close(); }
|
从缓存中刷出数据
4.2.2
参数名 | 描述 |
---|---|
Flushable flushable | flushable {@link Flushable} |
//传统的写法
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
FileOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
//创建流
outputStream = new FileOutputStream(dest);
//新new OutputStreamWriter对象,记得关闭回收
outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
String content = "1hello 小虚竹\n2hello 小虚竹";
int c;
for (int i = 0; i < content.length(); i++) {
c = content.charAt(i);
outputStreamWriter.write((char) c);
}
outputStreamWriter.flush();
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
}finally {
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
//使用IoUtil.flush的写法
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
FileOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
//创建流
outputStream = new FileOutputStream(dest);
//新new OutputStreamWriter对象,记得关闭回收
outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
String content = "1hello 小虚竹\n2hello 小虚竹";
int c;
for (int i = 0; i < content.length(); i++) {
c = content.charAt(i);
outputStreamWriter.write((char) c);
}
IoUtil.flush(outputStreamWriter);
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
IoUtil.close(outputStream);
IoUtil.close(outputStreamWriter);
}
链接:待补充
关闭
关闭失败不会抛出异常
参数名 | 描述 |
---|---|
Closeable closeable | closeable 被关闭的对象 |
//传统的写法
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
FileOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
//创建流
outputStream = new FileOutputStream(dest);
//新new OutputStreamWriter对象,记得关闭回收
outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
String content = "1hello 小虚竹\n2hello 小虚竹";
int c;
for (int i = 0; i < content.length(); i++) {
c = content.charAt(i);
outputStreamWriter.write((char) c);
}
outputStreamWriter.flush();
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (outputStream != null) {
outputStream.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
}finally {
try {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
//使用IoUtil.close的写法
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
FileOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
//创建流
outputStream = new FileOutputStream(dest);
//新new OutputStreamWriter对象,记得关闭回收
outputStreamWriter = IoUtil.getUtf8Writer(outputStream);
String content = "1hello 小虚竹\n2hello 小虚竹";
int c;
for (int i = 0; i < content.length(); i++) {
c = content.charAt(i);
outputStreamWriter.write((char) c);
}
IoUtil.flush(outputStreamWriter);
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
IoUtil.close(outputStream);
IoUtil.close(outputStreamWriter);
}
链接:待补充
尝试关闭指定对象<br>
判断对象如果实现了{@link AutoCloseable},则调用之
4.3.2
参数名 | 描述 |
---|---|
Object obj | obj 可关闭对象 |
public class AutoCloseableObject implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("--close AutoCloseableObject--");
}
public void demo(){
System.out.println("这是AutoCloseableObject对象里的demo方法");
//1.7之后,只要实现了AutoCloseable接口
try (FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt")) {
System.out.println("--fileInputStream--");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//----------------------------------
//先了解下AutoCloseable的工作原理
//1、AutoCloseable jdk1.7+才支持
//2、try方法可以管理多个资源,用;号隔开
//3、管理的资源一定要实现AutoCloseable接口
try(AutoCloseableObject object = new AutoCloseableObject()) {
System.out.println("--执行closeIfPosibleTest方法--");
System.out.println("--demo--");
object.demo();
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
try (FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt")) {
System.out.println("--fileInputStream--");
IoUtil.closeIfPosible(fileInputStream);
System.out.println(fileInputStream);
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
链接:待补充
对比两个流内容是否相同<br>
内部会转换流为 {@link BufferedInputStream}
4.0.6
参数名 | 描述 |
---|---|
InputStream input1 | input1 第一个流 |
InputStream input2 | input2 第二个流 |
两个流的内容一致返回true,否则false
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;
try (FileInputStream srcFileInputStream = new FileInputStream(src);
FileInputStream destFileInputStream = new FileInputStream(dest)) {
Assert.assertEquals(Boolean.TRUE, IoUtil.contentEquals(srcFileInputStream,destFileInputStream));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
File flushFile = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
try (FileInputStream srcFileInputStream = new FileInputStream(src);
FileInputStream flushFileInputStream = new FileInputStream(flushFile)) {
Assert.assertEquals(Boolean.FALSE, IoUtil.contentEquals(srcFileInputStream,flushFileInputStream));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
链接:待补充
对比两个Reader的内容是否一致<br>
内部会转换流为 {@link BufferedInputStream}
4.0.6
参数名 | 描述 |
---|---|
Reader input1 | input1 第一个reader |
Reader input2 | input2 第二个reader |
两个流的内容一致返回true,否则false
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toCopyTest1.txt") ;
try (FileReader srcFileFileReader = new FileReader(src);
FileReader destFileFileReader = new FileReader(dest)) {
Assert.assertEquals(Boolean.TRUE, IoUtil.contentEquals(srcFileFileReader,destFileFileReader));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
File flushFile = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/flushTest.txt") ;
try (FileReader srcFileFileReader = new FileReader(src);
FileReader flushFileFileReader = new FileReader(flushFile)) {
Assert.assertEquals(Boolean.FALSE, IoUtil.contentEquals(srcFileFileReader,flushFileFileReader));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
链接:待补充
对比两个流内容是否相同,忽略EOL字符<br>
内部会转换流为 {@link BufferedInputStream}
4.0.6
参数名 | 描述 |
---|---|
Reader input1 | input1 第一个流 |
Reader input2 | input2 第二个流 |
两个流的内容一致返回true,否则false
//对比两个流内容是否相同,忽略EOL字符 像\n,\r 就是EOL字符
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/toContentEqualsIgnoreEOLTest1.txt") ;
try (FileReader srcFileFileReader = new FileReader(src);
FileReader destFileFileReader = new FileReader(dest)) {
Assert.assertEquals(Boolean.TRUE, IoUtil.contentEqualsIgnoreEOL(srcFileFileReader,destFileFileReader));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
//对比两个流内容是否相同,忽略EOL字符 像\n,\r 就是EOL字符
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
File flushFile = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest2.txt") ;
try (FileReader srcFileFileReader = new FileReader(src);
FileReader flushFileFileReader = new FileReader(flushFile)) {
Assert.assertEquals(Boolean.TRUE, IoUtil.contentEqualsIgnoreEOL(srcFileFileReader,flushFileFileReader));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
链接:待补充
计算流CRC32校验码,计算后关闭流
(CRC32是什么:CRC是一个数值,该数值被用于校验数据的正确性。类似的校验有MD5校验码)
4.0.6
参数名 | 描述 |
---|---|
InputStream in | in 文件,不能为目录 |
CRC32值
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
try {
FileInputStream srcFileInputStream = new FileInputStream(src);
//计算流的校验码,计算后关闭流
System.out.println( IoUtil.checksumCRC32(srcFileInputStream));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
链接:待补充
计算流的校验码,计算后关闭流
4.0.10
参数名 | 描述 |
---|---|
InputStream in | in 流 |
Checksum checksum | checksum {@link Checksum} |
Checksum
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
try {
FileInputStream srcFileInputStream = new FileInputStream(src);
//计算流的校验码,计算后关闭流,支持可选校验码 实现Checksum
System.out.println( IoUtil.checksum(srcFileInputStream,new CRC32()).getValue());
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
链接:待补充
计算流的校验码,计算后关闭流
5.4.0
参数名 | 描述 |
---|---|
InputStream in | in 流 |
Checksum checksum | checksum {@link Checksum} |
Checksum
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
try {
FileInputStream srcFileInputStream = new FileInputStream(src);
//计算流的校验码,计算后关闭流,支持可选校验码 实现Checksum
System.out.println( IoUtil.checksumValue(srcFileInputStream,new CRC32()));
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
}
链接:待补充
返回行遍历器
LineIterator it = null;
try {
it = IoUtil.lineIter(reader);
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}
5.6.1
参数名 | 描述 |
---|---|
Reader reader | reader {@link Reader} |
{@link LineIter}
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
LineIter it = null;
try (FileReader srcFileFileReader = new FileReader(src)) {
//返回行遍历器
it = IoUtil.lineIter(srcFileFileReader);
while (it.hasNext()) {
String line = it.nextLine();
System.out.println("行遍历器行数据:"+line);
}
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
it.close();
}
链接:待补充
返回行遍历器
LineIterator it = null;
try {
it = IoUtil.lineIter(in, CharsetUtil.CHARSET_UTF_8);
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
it.close();
}
5.6.1
参数名 | 描述 |
---|---|
InputStream in | in {@link InputStream} |
Charset charset | charset 编码 |
{@link LineIter}
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/contentEqualsIgnoreEOLTest1.txt") ;
LineIter it = null;
try (FileInputStream srcFileInputStream = new FileInputStream(src);) {
//返回行遍历器
it = IoUtil.lineIter(srcFileInputStream,CharsetUtil.CHARSET_UTF_8);
while (it.hasNext()) {
String line = it.nextLine();
System.out.println("行遍历器行数据:"+line);
}
}catch (Exception e){
System.out.println("--exception--");
}finally {
System.out.println("--finally--");
it.close();
}
链接:待补充
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://xiaoxuzhu.blog.csdn.net/article/details/117673626
内容来源于网络,如有侵权,请联系作者删除!