工具方法

  • 创建目录

org.apache.commons.io.FileUtils#forceMkdir(File directory)

  • 写文件

org.apache.commons.io.FileUtils#write(java.io.File, java.lang.CharSequence, java.lang.String, boolean)

普通文件读写

package com.io;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;/*** @author yanyugang* @description 文件读写例子* @date 2019-10-14 9:36*/
public class ReadWriteFile {private static final String path="D:/readwritefile.txt";public static void main(String[] args){// 写文件writeFileFun();// 读文件readFileFun();}private static void writeFileFun(){//待写入的内容StringBuffer buf=new StringBuffer();buf.append("Hello World!").append("\r\n");buf.append("你好,世界!").append("\r\n");buf.append("Hello Java!").append("\r\n");buf.append("你好,Java!").append("\r\n");File file = new File(path);if (!file.exists()) {file.getParentFile().mkdirs();}try (BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file, true))) {// 父级目录存在的情况下,文件才能创建成功bos.write(buf.toString().getBytes(Charset.defaultCharset()));bos.flush();} catch (Exception e) {e.printStackTrace();}}private static void readFileFun(){//待读取的内容try (BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File(path)))) {byte[] buf=new byte[1024];int len=0;System.out.println("------------------读取内容--------------------------");while ((len=bis.read(buf))!=-1) {System.out.println(new String(buf, 0, len));}} catch (Exception e) {e.printStackTrace();}}}

commons-io文件读写

package com.io;import org.apache.commons.io.FileUtils;import java.io.File;
import java.io.IOException;/*** @author yanyugang* @description commons-io实现文件读写例子* @date 2019-10-14 9:36*/
public class CommonsIoReadWriteFile {private static final String path="D:/day22/commonsioreadwritefile.txt";public static void main(String[] args){// 写文件writeFileFun();// 读文件readFileFun();}private static void writeFileFun(){//待写入的内容StringBuffer buf=new StringBuffer();buf.append("Hello World!").append("\r\n");buf.append("你好,世界!").append("\r\n");buf.append("Hello Java!").append("\r\n");buf.append("你好,Java!").append("\r\n");try {FileUtils.writeStringToFile(new File(path), buf.toString(), "UTF-8", true);} catch (IOException e) {e.printStackTrace();}}private static void readFileFun(){//待读取的内容System.out.println("------------------读取内容--------------------------");try {System.out.println(FileUtils.readFileToString(new File(path), "UTF-8"));} catch (IOException e) {e.printStackTrace();}}}

commons-io之FileUtils相关推荐

  1. 使用org.apache.commons.io.FileUtils,IOUtils工具类操作文件

    转载自 使用org.apache.commons.io.FileUtils,IOUtils;工具类操作文件 File src = new File("G:/2012/portal/login ...

  2. Commons IO

    Common IO 是一个工具库,用来帮助开发IO功能 它包括6个主要部分 Utility classes – 包括一些静态方法来执行常用任务 Input – InputStream 和 Reader ...

  3. [转]Apache Commons IO入门教程

    Apache Commons IO是Apache基金会创建并维护的Java函数库.它提供了许多类使得开发者的常见任务变得简单,同时减少重复(boiler-plate)代码,这些代码可能遍布于每个独立的 ...

  4. Java IO流学习总结八:Commons IO 2.5-IOUtils

    Java IO流学习总结八:Commons IO 2.5-IOUtils 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/550519 ...

  5. Java IO流学习总结七:Commons IO 2.5-FileUtils

    Java IO流学习总结七:Commons IO 2.5-FileUtils 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/5497 ...

  6. Commons IO方便读写文件的工具类

    Commons IO是apache的一个开源的工具包,封装了IO操作的相关类,使用Commons IO可以很方便的读写文件,url源代码等. 普通地读取一个网页的源代码的代码可能如下 InputStr ...

  7. Apache Commons IO教程:初学者指南

    Apache Commons IO是由Apache Foundation创建和维护的Java库. 它提供了许多类,使开发人员可以轻松地完成常见任务,并且减少样板代码 ,而每个项目都需要一遍又一遍地编写 ...

  8. 使用Apache Commons IO组件读取大文件

    Apache Commons IO读取文件代码如下: Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new ...

  9. java-Apache Commons IO

    Google的guava对Java的IO操作进行了一定封装,但是它更偏向于集合.并发和缓存,在实际项目中,我非常喜欢guava,同时我也非常喜欢Apache的一个工具包org.apache.commo ...

  10. IO与文件读写---使用Apache commons io包提高读写效率

    [一]Apache commons IO简介 首先贴一段Apache commons IO官网上的介绍,来对这个著名的开源包有一个基本的了解: Commons IO is a library of u ...

最新文章

  1. SAP Return to External Vendor
  2. uva437巴比伦塔
  3. web developer tips (37):如何组织Using指令
  4. MySQL 删除字段数据某关键字后的所有数据
  5. linux7怎么安装yum,centos7下怎么安装yum
  6. 创建型模式——抽象工厂模式
  7. 20190806:字符串解密
  8. java访问本地文件_详解Java读取本地文件并显示在JSP文件中
  9. 配置nginx作为下载站点
  10. JavaScript : 基本的处理事件
  11. 为什么程序员下班后只关显示器却从不关电脑?
  12. 米家推出新款石头机器人,扫拖一体,指哪去哪!
  13. wpf后台设置颜色(背景色,前景色)
  14. win10插入耳机没声音解决办法
  15. JavaScript 判断手机号码格式 正则表达式 亲测
  16. 果园大二下英方课学习姿势
  17. 【牛投客】;牛投客最新消息
  18. android类加载
  19. python快速将excel文件转化为txt文件
  20. Silverlight 2.5D RPG游戏技巧与特效处理:(三)动态光影

热门文章

  1. Unity调用打印机
  2. Redis进阶: 锁的使用
  3. android 开发地图,(android地图开发) 高德地图开发准备
  4. Python 修改图片的时候抗锯齿
  5. 威纶通触摸屏直接与台达变频器进行MODBUS RTU通信的具体方法(图文)
  6. 初步使用计算机教学案列,信息技术与学科教学整合教学案例
  7. gtx1050ti最稳定的驱动_GTX1050ti显卡驱动下载_NVIDIA GeForce GTX1050ti显卡驱动Win7Win10版下载 - 系统之家...
  8. Android设备运用Clockworkmod Recovery恢复模式安装定制的Rom
  9. Jersey入门教程
  10. 使用WireShark抓包对方QQ的ip地址(通过QQ电话)