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

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

image.pngimage.pngimage.pngimage.png

    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.png

    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.png

    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.pngimage.pngimage.png

    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.png

参考案例:

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

image.pngimage.pngimage.png

目录
相关文章
|
11月前
|
Java 数据库
使用hutool工具类轻松实现导入导出
只有代码,解释较少,想看详细导入导出:
386 0
|
Java
12《hutool实战》:FileTypeUtil 文件类型判断工具类(JAVA 小虚竹)
12《hutool实战》:FileTypeUtil 文件类型判断工具类(JAVA 小虚竹)
743 0
12《hutool实战》:FileTypeUtil 文件类型判断工具类(JAVA 小虚竹)
|
7月前
|
XML 编解码 算法
Hutool超级工具类
Hutool超级工具类
56 0
|
JSON Java 数据格式
Java:Hutool工具箱之hutool-jsonJSON数据读取转换处理
Java:Hutool工具箱之hutool-jsonJSON数据读取转换处理
1111 0
|
Java Spring
hutool工具类处理工具
hutool工具类处理工具
8hutool实战:FileUtil 文件工具类(获取输入流)
8hutool实战:FileUtil 文件工具类(获取输入流)
303 0
8hutool实战:FileUtil 文件工具类(获取输入流)
9hutool实战:FileUtil 文件工具类(读取文件)
9hutool实战:FileUtil 文件工具类(读取文件)
1717 0
9hutool实战:FileUtil 文件工具类(读取文件)
|
安全
3hutool实战:IoUtil 流操作工具类(从流中读取内容)
3hutool实战:IoUtil 流操作工具类(从流中读取内容)
757 0
3hutool实战:IoUtil 流操作工具类(从流中读取内容)