1.FileUtils介绍

文件IO是我们日常项目中经常使用到的基础API,常见的IO读写操作基础类字节流InputStream与OutputStream、字符流Reader与Writer已经涵盖了我们日常项目开发中的常见API功能。具体的基础回顾可以参见一篇cdsn博文:字符流与字节流的区别

        今天要说的,是我们基于上述API由Apache开源项目封装的一个更好用的文件操作工具类FileUtils,涵盖了读取文件、拷贝文件、拷贝目录及文件、删除目录及文件、清除目录等比较常用的静态类封装。

  FileUtils的使用需要引入apache的commons-io包下的FileUtils,导入:import org.apache.commons.io.FileUtils;

  官方API文档:http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html

  maven项目使用需要导入依赖:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>

下面简单介绍一下它常见的API方法的使用。

1.1.读取文件

官方API中我觉得能使用到的就是下面的方法,都是静态方法,如readFileToString、readLines(按行读取)等,对应的静态方法也都提供了支持编码"utf-8"格式读取。

 

 1.2.拷贝文件

比较常用的API方法如下,如copyFile、copyInputStreamToFile、copyFileToDirectory。

 ①拷贝文件到文件copyFile(File srcFile, File destFile)

File file = new File("E:\\java\\file01\\abc雪.jpg");
String destFilePath = "E:\\java\\file02";
String destFileName = "abc雪02.jpg";
try {
    FileUtils fileUtils = new FileUtils();
    //文件拷贝到新的位置并保存文件的日期。
    fileUtils.copyFile(file, new File(destFilePath,destFileName));
    System.out.println("文件拷贝成功");
} catch (IOException e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
}

拷贝文件到字节输出流copyFile(File input, OutputStream output)

String destFileName = "abc雪03.jpg";
//从文件copy to an字节输出流。
FileUtils.copyFile(file, new FileOutputStream(new File(destFilePath,destFileName)));

拷贝文件到文件的目录保存文件的日期copyFileToDirectory(File srcFile, File destDir)

拷贝的文件名无法自定义,和原文件名一样

//拷贝文件到文件的目录保持文件的日期。
FileUtils.copyFileToDirectory(file, new File(destFilePath));

 

1.3.拷贝目录及文件

主要的API如下,包含拷贝目录及目录下的子目录及文件的拷贝方法:

 

 ①将整个目录拷贝到新位置,并保持原文件日期copyDirectory(File srcDir, File destDir)

     其包含文件及子目录文件并保持原文件日期

File file = new File("E:\\java\\file01");
String destFilePath = "E:\\java\\file03";
try {
    //将整个目录复制新位置,并保持原文件日期。
    FileUtils.copyDirectory(file, new File(destFilePath));
    System.out.println("文件目录拷贝成功");
} catch (IOException e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
}

 ②将已筛选的目录拷贝到新位置

copyDirectory(File srcDir, File destDir, FileFilter filter)

文件过滤器筛选其包含文件及子目录文件拷贝,并保持原文件日期。

String destFilePath = "E:\\java\\file04";
//将已筛选的目录复制,并保持原文件日期的新位置。
FileUtils.copyDirectory(file, new File(destFilePath), new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        if(pathname.isDirectory()) return true;
        else {
            boolean b1 = pathname.getName().endsWith(".txt");
            boolean b2 = pathname.getName().endsWith(".jpg");
            return b1 || b2;
        }
    }
});

 

1.4.删除目录及文件

主要的删除API方法如下,常用的有deleteDirectory、deleteQuietly及forceDelete等,主要用于级联删除及强制删除,且不会引起异常。非常强力。

 ①删除指定文件,从不引发异常deleteQuietly(File file)

File file = new File("E:\\java\\file04\\abc雪.jpg");
//删除指定文件,从不引发异常。
FileUtils.deleteQuietly(file);

删除指定文件,文件不存在抛出异常forceDelete(File file)

File file = new File("E:\\java\\file04\\abc雪.jpg");
try {
    FileUtils.forceDelete(file);
    System.out.println("操作成功");
} catch (IOException e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
}

递归删除目录deleteDirectory(File directory)

删除其包含文件及子目录文件

File file = new File("E:\\java\\file04\\abc雪.jpg");
//递归删除目录。
try {
    FileUtils.deleteDirectory(new File(destFilePath));
    System.out.println("操作成功");
} catch (IOException e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
}

1.5.清除目录

主要的API如下,主要用于清除当前目录下(不会删除到当前目录级别)的文件夹及其包含的文件。

 清除该目录下的文件及子目录文件而不删除该目录文件夹。该目录不存在会报错:

String destFilePath = "E:\\java\\file04";
try {
    FileUtils.cleanDirectory(new File(destFilePath));
    System.out.println("操作成功");
} catch (IOException e) {
    e.printStackTrace();
    System.out.println(e.getMessage());
}

1.6.文件大小计数常量

①EMPTY_FILE_ARRAY,空文件数组:

/**
   * An empty array of type {@code File}.
   */
public static final File[] EMPTY_FILE_ARRAY = {};

②1GB大小,分别对应long类型及BigInteger包装类型

/**
   * The number of bytes in a gigabyte.
   */
public static final long ONE_GB = ONE_KB * ONE_MB;

 /**
    * The number of bytes in a gigabyte.
    *
    * @since 2.4
    */
public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI);

③同理KB与MB的显示类型:

/**
     * The number of bytes in a kilobyte.
     */
public static final long ONE_KB = 1024;

/**
   * The number of bytes in a kilobyte.
   *
   * @since 2.4
   */
public static final BigInteger ONE_KB_BI = BigInteger.valueOf(ONE_KB);

/**
   * The number of bytes in a megabyte.
   */
public static final long ONE_MB = ONE_KB * ONE_KB;

 

 博文转载至CSDN博客:

FileUtils工具类常用方法

基于博主博文我自己做了一小部分的知识补充,也非常感谢博主提供博文分享学习知识的平台。

站在巨人的肩膀上,我们才能学到更多的知识~

 

posted on 2021-10-31 22:08  人无名,则可专心练剑  阅读(2671)  评论(0编辑  收藏  举报