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

34 阅读5分钟

获得一个输出流对象

支持版本及以上

参数描述:

参数名描述
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);
		}

源码解析:

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!