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

x33g5p2x  于2021-12-18 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(673)

用途: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 filefile 文件

返回值:

输出流对象

参考案例:

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);
		}

源码解析:

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

方法明细

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

方法描述

获得一个输出流对象

支持版本及以上

参数描述:

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

返回值:

输出流对象

参考案例:

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);
		}

源码解析:

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

方法明细

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

方法描述

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

支持版本及以上

参数描述:

参数名描述
String pathpath 输出路径,绝对路径
String charsetNamecharsetName 字符集
boolean isAppendisAppend 是否追加

返回值:

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);
		}

源码解析:

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

方法明细

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

方法描述

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

支持版本及以上

参数描述:

参数名描述
String pathpath 输出路径,绝对路径
Charset charsetcharset 字符集
boolean isAppendisAppend 是否追加

返回值:

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);
		}

源码解析:

链接:待补充

方法明细

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

方法描述

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

支持版本及以上

参数描述:

参数名描述
File filefile 输出文件
String charsetNamecharsetName 字符集
boolean isAppendisAppend 是否追加

返回值:

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 filefile 输出文件
Charset charsetcharset 字符集
boolean isAppendisAppend 是否追加

返回值:

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 pathpath 输出路径,绝对路径
String charsetcharset 字符集
boolean isAppendisAppend 是否追加

返回值:

打印对象

参考案例:

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);
		}

源码解析:

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

方法明细

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

方法描述

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

支持版本及以上

4.1.1

参数描述:

参数名描述
String pathpath 输出路径,绝对路径
Charset charsetcharset 字符集
boolean isAppendisAppend 是否追加

返回值:

打印对象

参考案例:

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 filefile 文件
String charsetcharset 字符集
boolean isAppendisAppend 是否追加

返回值:

打印对象

参考案例:

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 filefile 文件
Charset charsetcharset 字符集
boolean isAppendisAppend 是否追加

返回值:

打印对象

参考案例:

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);
		}

源码解析:

链接:待补充

相关文章