文章23 | 阅读 31898 | 点赞0
IO工具类只是辅助流的读写,并不负责关闭流。原因是流可能被多次读写,读写关闭后容易造成问题。
(获取getReader and getWriter)
(获取getReader and getWriter)
(获取getReader and getWriter)
此博文的依据:hutool-5.6.5版本源码
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
<version>5.6.5</version>
</dependency>
方法 | 描述 |
---|---|
cn.hutool.core.io.IoUtil.getUtf8Reader(java.io.InputStream) | 获得一个文件读取器,默认使用UTF-8编码 |
cn.hutool.core.io.IoUtil.getReader(java.io.InputStream, java.lang.String) | 获得一个文件读取器 |
cn.hutool.core.io.IoUtil.getReader(cn.hutool.core.io.BOMInputStream) | 从{@link BOMInputStream}中获取Reader |
cn.hutool.core.io.IoUtil.getReader(java.io.InputStream, java.nio.charset.Charset) | 获得一个Reader |
cn.hutool.core.io.IoUtil.getReader(java.io.Reader) | 获得{@link BufferedReader}<br> 如果是{@link BufferedReader}强转返回,否则新建。如果提供的Reader为null返回null |
cn.hutool.core.io.IoUtil.getPushBackReader(java.io.Reader, int) | 获得{@link PushbackReader}<br> 如果是{@link PushbackReader}强转返回,否则新建 |
cn.hutool.core.io.IoUtil.getUtf8Writer(java.io.OutputStream) | 获得一个Writer,默认编码UTF-8 |
cn.hutool.core.io.IoUtil.getWriter(java.io.OutputStream, java.lang.String) | 获得一个Writer |
cn.hutool.core.io.IoUtil.getWriter(java.io.OutputStream, java.nio.charset.Charset) | 获得一个Writer |
获得一个文件读取器,默认使用UTF-8编码
5.1.6
参数名 | 描述 |
---|---|
InputStream in | in 输入流 |
BufferedReader对象
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
FileInputStream fw = null;
BufferedReader bufferedReader = null;
try {
//创建流
fw = new FileInputStream(src);
//新new BufferedReader对象,记得关闭回收
bufferedReader = IoUtil.getUtf8Reader(fw);
String str = null;
//到达流末尾, 就返回null
while((str = bufferedReader.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
}finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (fw != null) {
fw.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
}finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
链接:待补充
获得一个文件读取器
参数名 | 描述 |
---|---|
InputStream in | in 输入流 |
String charsetName | charsetName 字符集名称 |
BufferedReader对象
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
FileInputStream fw = null;
BufferedReader bufferedReader = null;
try {
//创建流
fw = new FileInputStream(src);
//新new BufferedReader对象,记得关闭回收
bufferedReader = IoUtil.getReader(fw, "UTF-8");
String str = null;
//到达流末尾, 就返回null
while((str = bufferedReader.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (fw != null) {
fw.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
链接:待补充
从{@link BOMInputStream}中获取Reader
5.5.8
参数名 | 描述 |
---|---|
BOMInputStream in | in {@link BOMInputStream} |
{@link BufferedReader}
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
//BOM(Byte Order Mark)标识文件的编码,实际大小比数据多3个字节
// 直接在记事本编辑数据保存,默认会给你的数据添加上BOM头,使你的文件的大小比实际数据多3个字节(utf-8编码)
BOMInputStream fw = null;
BufferedReader bufferedReader = null;
try {
//创建流
fw = new BOMInputStream(new FileInputStream(src));
//新new BufferedReader对象,记得关闭回收
bufferedReader = IoUtil.getReader(fw);
String str = null;
//到达流末尾, 就返回null
while((str = bufferedReader.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (fw != null) {
fw.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
}finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
链接:待补充
获得一个Reader
参数名 | 描述 |
---|---|
InputStream in | in 输入流 |
Charset charset | charset 字符集 |
BufferedReader对象
File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/copyTest1.txt") ;
FileInputStream fw = null;
BufferedReader bufferedReader =null;
try {
//创建流
fw = new FileInputStream(src);
//新new BufferedReader对象,记得关闭回收
bufferedReader = IoUtil.getReader(fw,CharsetUtil.CHARSET_UTF_8);
String str = null;
//到达流末尾, 就返回null
while((str = bufferedReader.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (fw != null) {
fw.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
}finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
链接:待补充
获得{@link BufferedReader}<br>
如果是{@link BufferedReader}强转返回,否则新建。如果提供的Reader为null返回null
3.0.9
参数名 | 描述 |
---|---|
Reader reader | reader 普通Reader,如果为null返回null |
{@link BufferedReader} or null
StringReader stringReader = null;
BufferedReader bufferedReader =null;
try {
//创建流
stringReader = new StringReader("1hello 小虚竹\n2hello 小虚竹");
//新new BufferedReader对象,记得关闭回收
bufferedReader = IoUtil.getReader(stringReader);
String str = null;
//到达流末尾, 就返回null
while((str = bufferedReader.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (stringReader != null) {
stringReader.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
}finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
链接:待补充
获得{@link PushbackReader}<br>
如果是{@link PushbackReader}强转返回,否则新建
3.1.0
参数名 | 描述 |
---|---|
Reader reader | reader 普通Reader |
int pushBackSize | pushBackSize 推后的byte数 |
{@link PushbackReader}
StringReader stringReader = null;
PushbackReader pushbackReader = null;
try {
stringReader = new StringReader("123456789");
//PushbackReader允许调用者将一些数据源本身不包含的字符插入到流的任意位置
pushbackReader = IoUtil.getPushBackReader(stringReader,100);
StringBuilder stringBuilder = new StringBuilder();
char[] buff = new char[3];
//第一步,首先读取 “123”
int n = pushbackReader.read(buff, 0, 3);
stringBuilder.append(buff);
System.out.println("第一步,读取了 " + n + " 个字符:");
System.out.println(buff);
//第二步,unread:abc, de, fghi
pushbackReader.unread(new char[]{'a', 'b', 'c'});
pushbackReader.unread(new char[]{'d', 'e'});
pushbackReader.unread(new char[]{'f', 'g', 'h', 'i'});
int c = 0;
while ((c = pushbackReader.read()) != -1){
stringBuilder.append((char) c);
}
System.out.println("最终读取的数据:" + stringBuilder.toString());
}catch (IOException e){
//抛出一个运行时异常(直接停止掉程序)
throw new RuntimeException("运行时异常",e);
} finally {
try {
//如果是空的 说明流创建失败 失败了不需要关闭
if (stringReader != null) {
stringReader.close();
}
} catch (Exception e) {
//关闭资源失败 停止程序
throw new RuntimeException("关闭资源失败");
}finally {
try {
if (pushbackReader != null) {
pushbackReader.close();
}
} catch (Exception e) {
throw new RuntimeException("关闭资源失败");
}
}
}
链接:待补充
获得一个Writer,默认编码UTF-8
5.1.6
参数名 | 描述 |
---|---|
OutputStream out | out 输入流 |
OutputStreamWriter对象
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/getUtf8WriterTest.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("关闭资源失败");
}
}
}
链接:待补充
获得一个Writer
参数名 | 描述 |
---|---|
OutputStream out | out 输入流 |
String charsetName | charsetName 字符集 |
OutputStreamWriter对象
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/getWriterTest.txt") ;
FileOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
//创建流
outputStream = new FileOutputStream(dest);
//新new OutputStreamWriter对象,记得关闭回收
outputStreamWriter = IoUtil.getWriter(outputStream,"UTF-8");
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("关闭资源失败");
}
}
}
链接:待补充
获得一个Writer
参数名 | 描述 |
---|---|
OutputStream out | out 输入流 |
Charset charset | charset 字符集 |
OutputStreamWriter对象
File dest = new File("C:\\Users\\Administrator\\Desktop\\xuzhu/getWriterTest1.txt") ;
FileOutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
try {
//创建流
outputStream = new FileOutputStream(dest);
//新new OutputStreamWriter对象,记得关闭回收
outputStreamWriter = IoUtil.getWriter(outputStream,CharsetUtil.CHARSET_UTF_8);
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("关闭资源失败");
}
}
}
链接:待补充
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://xiaoxuzhu.blog.csdn.net/article/details/117672993
内容来源于网络,如有侵权,请联系作者删除!