org.apache.commons.io.FileUtils例子

jopen 10年前

用apache.commons.io 中的一些方法来操作文件,会减轻程序员的很多负担,文件操作经常会用到FileUtils这个类,今天在这里对最常用的一些方法做一个总结,当然最全面的东西,还是应该看apache.commons.io 的文档或者示例。

contentEquals(File file1, File file2): 比较两个文件的内容,返回他们的内容是否相同.
copyDirectory(File source, File destination):从一个文件夹拷贝内容到另外一个文件夹,包括子目录。
copyDirectory(File source, File destination, FileFilter filter): 基本用途类似,但多了一个过滤条件,只拷贝满足条件的文件或子文件夹,比如根据名称,修改时间等过滤
copyFile(File srcFile, File destFile): 拷贝文件
deleteDirectory(File directory):  递归删除文件夹.
getTempDirectory(): 返回系统临时目录.
getUserDirectory(): 返回用户目录.
lineIterator(File file): 返回给定文件,所有的行,可以对所有的行进行循环操作
sizeOfDirectory(File directory): 返回文件夹大小.
writeLines(File file, Collection<?> lines): 写文件,将一个集合里的内容都写到文件中去.

import java.io.File;  import java.io.IOException;  import java.util.ArrayList;  import java.util.Collection;  import org.apache.commons.io.FileUtils;  import org.apache.commons.io.LineIterator;  import org.apache.commons.io.filefilter.SuffixFileFilter;  public class FileUtilsExample {            private static final String MAIN_PATH = "C:\\Users\\Lilykos\\Desktop\\FUExFiles\\";      public static void main(String[] args) throws IOException {                    // file1: "Hello World!"          // file2: "Hello World!!!"          File file1 = FileUtils.getFile(MAIN_PATH + "cmpFile1.txt");          File file2 = FileUtils.getFile(MAIN_PATH + "cmpFile2.txt");          System.out.println("Are cmpFile1 and cmpFile2 equal: " +                  FileUtils.contentEquals(file1, file2));                              // We can copy a whole path somewhere          // and check if the operation was successful.          FileUtils.copyDirectory(                  FileUtils.getFile(MAIN_PATH),                   // source                  FileUtils.getFile(MAIN_PATH + "copiedPath\\")); // destination                    System.out.println("Does the copiedPath exist: " +                  FileUtils.getFile(MAIN_PATH + "copiedPath\\").exists());                              // Copy a directory by filtering out all the txt files.          // In the directory right now: cmpFile1.txt, cmpFile2.txt, zipFile.zip          FileUtils.copyDirectory(                  FileUtils.getFile(MAIN_PATH),                  FileUtils.getFile(MAIN_PATH + "copiedFilterPath\\"),                  new SuffixFileFilter(".zip"));                    for (File f: FileUtils.getFile(MAIN_PATH + "copiedFilterPath\\").listFiles()) {              System.out.println("Contents of copiedFilterPath: " + f.getName());          }                              // Copy a file          File copy = FileUtils.getFile(MAIN_PATH + "copyOfFile1.txt");          FileUtils.copyFile(file1, copy);          System.out.println("Are cmpFile1 and copyOfFile1 equal: " +                  FileUtils.contentEquals(file1, copy));                              // Right now there are these files in the MAIN_PATH.          for (File f: FileUtils.getFile(MAIN_PATH).listFiles()) {              System.out.println("Contents of MAIN_PATH: " + f.getName());          }          // Let's delete a directory and see the results.          FileUtils.deleteDirectory(FileUtils.getFile(MAIN_PATH + "copiedFilterPath\\"));          for (File f: FileUtils.getFile(MAIN_PATH).listFiles()) {              System.out.println("Contents of MAIN_PATH after deletion: " + f.getName());          }                              // Directories          System.out.println("Temp Dir: " +                  FileUtils.getTempDirectory().getAbsolutePath());          System.out.println("User Dir: " +                  FileUtils.getUserDirectory().getAbsolutePath());                              // Line Iterator          LineIterator iter = FileUtils.lineIterator(file1);          while (iter.hasNext()) {              System.out.println("cmpFile1 lines: " + iter.next());          }                              // Directory size          System.out.println("Size of dir: " +                  FileUtils.sizeOfDirectory(FileUtils.getFile(MAIN_PATH)) +                  " bytes.");                              // Ways of writing lines to a file.          File fileToWrite1 = FileUtils.getFile(MAIN_PATH + "fileToWrite1.txt");          File fileToWrite2 = FileUtils.getFile(MAIN_PATH + "fileToWrite2.txt");                    Collection lines = new ArrayList<>();          lines.add("Written with FileUtils!");          lines.add("For Java Code Geeks.");                    FileUtils.write(fileToWrite1, "Written with FileUtils!");          FileUtils.writeLines(fileToWrite2, lines);                    iter = FileUtils.lineIterator(fileToWrite1);          while (iter.hasNext()) {              System.out.println("fileToWrite1 lines: " + iter.next());          }          iter = FileUtils.lineIterator(fileToWrite2);          while (iter.hasNext()) {              System.out.println("fileToWrite2 lines: " + iter.next());          }      }  }


结果如下:
Are cmpFile1 and cmpFile2 equal: false  Does the copiedPath exist: true  Contents of copiedFilterPath: zipFile.zip  Are cmpFile1 and copyOfFile1 equal: true  Contents of MAIN_PATH: cmpFile1.txt  Contents of MAIN_PATH: cmpFile2.txt  Contents of MAIN_PATH: copiedFilterPath  Contents of MAIN_PATH: copiedPath  Contents of MAIN_PATH: copyOfFile1.txt  Contents of MAIN_PATH: fileToWrite1.txt  Contents of MAIN_PATH: fileToWrite2.txt  Contents of MAIN_PATH: zipFile.zip  Contents of MAIN_PATH after deletion: cmpFile1.txt  Contents of MAIN_PATH after deletion: cmpFile2.txt  Contents of MAIN_PATH after deletion: copiedPath  Contents of MAIN_PATH after deletion: copyOfFile1.txt  Contents of MAIN_PATH after deletion: fileToWrite1.txt  Contents of MAIN_PATH after deletion: fileToWrite2.txt  Contents of MAIN_PATH after deletion: zipFile.zip  Temp Dir: C:\Users\Lilykos\AppData\Local\Temp  User Dir: C:\Users\Lilykos  cmpFile1 lines: Hello World!  Size of dir: 304 bytes.  fileToWrite1 lines: Written with FileUtils!  fileToWrite2 lines: Written with FileUtils!  fileToWrite2 lines: For Java Code Geeks.
原文地址:http://www.yihaomen.com/article/java/546.htm,