10hutool实战:FileUtil 文件工具类(获取输出流)

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

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

用途:FileUtil 文件工具类(获取输出流)

使用场景

获取不同的输出流

项目引用

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

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

方法摘要

方法描述
cn.hutool.core.io.FileUtil.getOutputStream(java.io.File)
获得一个输出流对象
cn.hutool.core.io.FileUtil.getOutputStream(java.lang.String)
获得一个输出流对象
cn.hutool.core.io.FileUtil.getWriter(java.lang.String, java.lang.String, boolean)
获得一个带缓存的写入对象
cn.hutool.core.io.FileUtil.getWriter(java.lang.String, java.nio.charset.Charset, boolean)
获得一个带缓存的写入对象
cn.hutool.core.io.FileUtil.getWriter(java.io.File, java.lang.String, boolean)
获得一个带缓存的写入对象
cn.hutool.core.io.FileUtil.getWriter(java.io.File, java.nio.charset.Charset, boolean)
获得一个带缓存的写入对象
cn.hutool.core.io.FileUtil.getPrintWriter(java.lang.String, java.lang.String, boolean)
获得一个打印写入对象,可以有print
cn.hutool.core.io.FileUtil.getPrintWriter(java.lang.String, java.nio.charset.Charset, boolean)
获得一个打印写入对象,可以有print
cn.hutool.core.io.FileUtil.getPrintWriter(java.io.File, java.lang.String, boolean)
获得一个打印写入对象,可以有print
cn.hutool.core.io.FileUtil.getPrintWriter(java.io.File, java.nio.charset.Charset, boolean)
获得一个打印写入对象,可以有print

方法明细

方法名称:cn.hutool.core.io.FileUtil.getOutputStream(java.io.File)

方法描述

获得一个输出流对象

支持版本及以上

参数描述:

参数名描述
File file
file 文件

返回值:

输出流对象

参考案例:

		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu\\getOutputStreamTest.txt") ;
		BufferedOutputStream bufferedOutputStream = null;

		try {
			//创建流 src文件如果不存在,程序会帮忙创建
			bufferedOutputStream = FileUtil.getOutputStream(src);
			String str = "getOutputStreamTest内容1 \ngetOutputStreamTest内容2";
			byte[] sb = str.getBytes();
			bufferedOutputStream.write(sb);
			bufferedOutputStream.flush();
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(bufferedOutputStream);
		}

image-20210630657817

源码解析:

IoUtil.toBuffered和FileUtil.getOutputStream对比下
链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getOutputStream(java.lang.String)

方法描述

获得一个输出流对象

支持版本及以上

参数描述:

参数名描述
String path
path 输出到的文件路径,绝对路径

返回值:

输出流对象

参考案例:

		BufferedOutputStream bufferedOutputStream = null;

		try {
			//创建流 src文件如果不存在,程序会帮忙创建
			bufferedOutputStream = FileUtil.getOutputStream("C:\\Users\\Administrator\\Desktop\\xuzhu\\getOutputStreamTest1.txt");
			String str = "getOutputStreamTest内容1 \ngetOutputStreamTest内容2";
			byte[] sb = str.getBytes();
			bufferedOutputStream.write(sb);
			bufferedOutputStream.flush();
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(bufferedOutputStream);
		}

image-202106306627339

源码解析:

IoUtil.toBuffered和FileUtil.getOutputStream对比下
链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getWriter(java.lang.String, java.lang.String, boolean)

方法描述

获得一个带缓存的写入对象

支持版本及以上

参数描述:

参数名描述
String path
path 输出路径,绝对路径
String charsetName
charsetName 字符集
boolean isAppend
isAppend 是否追加

返回值:

BufferedWriter对象

参考案例:


		BufferedWriter bufferedWriter = null;
		try {
			//是否追加
			Boolean isAppend = false;
			//创建流
			bufferedWriter = FileUtil.getWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getWriterTest.txt",CharsetUtil.UTF_8,isAppend);
			String str = "getWriterTest1 \ngetWriterTest2";
			bufferedWriter.write(str);
			bufferedWriter.flush();
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(bufferedWriter);
		}

image-202106306409815

源码解析:

IoUtil.toBuffered和FileUtil.getWriter对比下
链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getWriter(java.lang.String, java.nio.charset.Charset, boolean)

方法描述

获得一个带缓存的写入对象

支持版本及以上

参数描述:

参数名描述
String path
path 输出路径,绝对路径
Charset charset
charset 字符集
boolean isAppend
isAppend 是否追加

返回值:

BufferedWriter对象

参考案例:

		BufferedWriter bufferedWriter = null;
		try {
			//是否追加
			Boolean isAppend = true;
			//创建流
			bufferedWriter = FileUtil.getWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getWriterTest.txt",CharsetUtil.CHARSET_UTF_8,isAppend);
			String str = "getWriterTest1 \ngetWriterTest2";
			bufferedWriter.write(str);
			bufferedWriter.flush();
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(bufferedWriter);
		}

image-202106306742810

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getWriter(java.io.File, java.lang.String, boolean)

方法描述

获得一个带缓存的写入对象

支持版本及以上

参数描述:

参数名描述
File file
file 输出文件
String charsetName
charsetName 字符集
boolean isAppend
isAppend 是否追加

返回值:

BufferedWriter对象

参考案例:


		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu\\getWriterTest.txt");
		BufferedWriter bufferedWriter = null;
		try {
			//是否追加
			Boolean isAppend = false;
			//创建流
			bufferedWriter = FileUtil.getWriter(src,CharsetUtil.UTF_8,isAppend);
			String str = "getWriterTest1 \ngetWriterTest2";
			bufferedWriter.write(str);
			bufferedWriter.flush();
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(bufferedWriter);
		}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getWriter(java.io.File, java.nio.charset.Charset, boolean)

方法描述

获得一个带缓存的写入对象

支持版本及以上

参数描述:

参数名描述
File file
file 输出文件
Charset charset
charset 字符集
boolean isAppend
isAppend 是否追加

返回值:

BufferedWriter对象

参考案例:

		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu\\getWriterTest.txt");
		BufferedWriter bufferedWriter = null;
		try {
			//是否追加
			Boolean isAppend = false;
			//创建流
			bufferedWriter = FileUtil.getWriter(src,CharsetUtil.CHARSET_UTF_8,isAppend);
			String str = "getWriterTest1 \ngetWriterTest2";
			bufferedWriter.write(str);
			bufferedWriter.flush();
		} catch (IOException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(bufferedWriter);
		}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getPrintWriter(java.lang.String, java.lang.String, boolean)

方法描述

获得一个打印写入对象,可以有print

支持版本及以上

参数描述:

参数名描述
String path
path 输出路径,绝对路径
String charset
charset 字符集
boolean isAppend
isAppend 是否追加

返回值:

打印对象

参考案例:

		PrintWriter printWriter = null;
		try {
			//是否追加
			Boolean isAppend = false;
			//创建流
			printWriter = FileUtil.getPrintWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getPrintWriterTest1.txt",CharsetUtil.UTF_8,isAppend);
			printWriter.write("小虚竹");
			printWriter.append(" 你真帅~");
			//写入时换行
			printWriter.println("我稀罕你");
			printWriter.write("我爱你");

			printWriter.flush();
		} catch (IORuntimeException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(printWriter);
		}

image-20210630193358105

源码解析:

PrintWriter的源码解析
链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getPrintWriter(java.lang.String, java.nio.charset.Charset, boolean)

方法描述

获得一个打印写入对象,可以有print

支持版本及以上

4.1.1

参数描述:

参数名描述
String path
path 输出路径,绝对路径
Charset charset
charset 字符集
boolean isAppend
isAppend 是否追加

返回值:

打印对象

参考案例:

		PrintWriter printWriter = null;
		try {
			//是否追加
			Boolean isAppend = false;
			//创建流
			printWriter = FileUtil.getPrintWriter("C:\\Users\\Administrator\\Desktop\\xuzhu\\getPrintWriterTest1.txt",CharsetUtil.CHARSET_UTF_8,isAppend);
			printWriter.write("小虚竹");
			printWriter.append(" 你真帅~");
			//写入时换行
			printWriter.println("我稀罕你");
			printWriter.write("我爱你");

			printWriter.flush();
		} catch (IORuntimeException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(printWriter);
		}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getPrintWriter(java.io.File, java.lang.String, boolean)

方法描述

获得一个打印写入对象,可以有print

支持版本及以上

参数描述:

参数名描述
File file
file 文件
String charset
charset 字符集
boolean isAppend
isAppend 是否追加

返回值:

打印对象

参考案例:

		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu\\getPrintWriterTest1.txt");
		PrintWriter printWriter = null;
		try {
			//是否追加
			Boolean isAppend = false;
			//创建流
			printWriter = FileUtil.getPrintWriter(src,CharsetUtil.UTF_8,isAppend);
			printWriter.write("小虚竹");
			printWriter.append(" 你真帅~");
			//写入时换行
			printWriter.println("我稀罕你");
			printWriter.write("我爱你");

			printWriter.flush();
		} catch (IORuntimeException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(printWriter);
		}

源码解析:

链接:待补充

方法明细

方法名称:cn.hutool.core.io.FileUtil.getPrintWriter(java.io.File, java.nio.charset.Charset, boolean)

方法描述

获得一个打印写入对象,可以有print

支持版本及以上

5.4.3

参数描述:

参数名描述
File file
file 文件
Charset charset
charset 字符集
boolean isAppend
isAppend 是否追加

返回值:

打印对象

参考案例:

		File src = new File("C:\\Users\\Administrator\\Desktop\\xuzhu\\getPrintWriterTest1.txt");
		PrintWriter printWriter = null;
		try {
			//是否追加
			Boolean isAppend = false;
			//创建流
			printWriter = FileUtil.getPrintWriter(src,CharsetUtil.CHARSET_UTF_8,isAppend);
			printWriter.write("小虚竹");
			printWriter.append(" 你真帅~");
			//写入时换行
			printWriter.println("我稀罕你");
			printWriter.write("我爱你");

			printWriter.flush();
		} catch (IORuntimeException e) {
			//抛出一个运行时异常(直接停止掉程序)
			throw new RuntimeException("运行时异常",e);
		}finally {
			IoUtil.close(printWriter);
		}

源码解析:

链接:待补充
Hutool提供了FileUtil工具类来读取文件。你可以使用FileUtil.readUtf8String方法来直接读取文件中的所有内容。首先,你需要在你的项目中引入Hutool的相关依赖,如下所示: ```xml <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.1.0</version> </dependency> ``` 然后,在你的代码中使用FileUtil.readUtf8String方法,并传入文件路径作为参数,即可读取文件的内容。示例代码如下: ```java String content = FileUtil.readUtf8String("文件路径"); ``` 这样,你就可以通过Hutool读取文件了。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [文件类操作---hutool工具](https://blog.csdn.net/weixin_42352733/article/details/122299819)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [9hutool实战:FileUtil 文件工具类(读取文件)](https://blog.csdn.net/shi_hong_fei_hei/article/details/118162754)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小虚竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值