JAVA資格認證:Java文件操作增強工具

字號:

Java的文件操作太基礎,缺乏很多實用工具,比如對目錄的操作,支持就非常的差了。如果你經(jīng)常用Java操作文件或文件夾,你會覺得反復編寫這些代碼是令人沮喪的問題,而且要大量用到遞歸。
    下面是的一個解決方案,借助Apache Commons IO工具包來簡單實現(xiàn)文件(夾)的復制、移動、刪除、獲取大小等操作。
    import org.apache.commons.io.FileUtils;
    import org.apache.commons.io.filefilter.*;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import java.io.*;
    /**
    * 文件工具箱
    *
    */
    public final class FileToolkit {
    private static final Log log = LogFactory.getLog(FileToolkit.class);
    /**
    * 復制文件或者目錄,復制前后文件完全一樣。
    *
    * @param resFilePath 源文件路徑
    * @param distFolder 目標文件夾
    * @IOException 當操作發(fā)生異常時拋出
    */
    public static void copyFile(String resFilePath, String distFolder) throws IOException {
    File resFile = new File(resFilePath);
    File distFile = new File(distFolder);
    if (resFile.isDirectory()) {
    FileUtils.copyDirectoryToDirectory(resFile, distFile);
    } else if (resFile.isFile()) {
    FileUtils.copyFileToDirectory(resFile, distFile, true);
    }
    }
    /**
    * 刪除一個文件或者目錄
    *
    * @param targetPath 文件或者目錄路徑
    * @IOException 當操作發(fā)生異常時拋出
    */
    public static void deleteFile(String targetPath) throws IOException {
    File targetFile = new File(targetPath);
    if (targetFile.isDirectory()) {
    FileUtils.deleteDirectory(targetFile);
    } else if (targetFile.isFile()) {
    targetFile.delete();
    }
    }
    /**
    * 移動文件或者目錄,移動前后文件完全一樣,如果目標文件夾不存在則創(chuàng)建。
    *
    * @param resFilePath 源文件路徑
    * @param distFolder 目標文件夾
    * @IOException 當操作發(fā)生異常時拋出
    */
    public static void moveFile(String resFilePath, String distFolder) throws IOException {
    File resFile = new File(resFilePath);
    File distFile = new File(distFolder);
    if (resFile.isDirectory()) {
    FileUtils.moveDirectoryToDirectory(resFile, distFile, true);
    } else if (resFile.isFile()) {
    FileUtils.moveFileToDirectory(resFile, distFile, true);
    }
    }
    /**
    * 重命名文件或文件夾
    *
    * @param resFilePath 源文件路徑
    * @param newFileName 重命名
    * @return 操作成功標識
    */
    public static boolean renameFile(String resFilePath, String newFileName) {
    String newFilePath = StringToolkit.formatPath(StringToolkit.getParentPath(resFilePath) + "/" + newFileName);
    File resFile = new File(resFilePath);
    File newFile = new File(newFilePath);
    return resFile.renameTo(newFile);
    }
    /**
    * 讀取文件或者目錄的大小
    *
    * @param distFilePath 目標文件或者文件夾
    * @return 文件或者目錄的大小,如果獲取失敗,則返回-1
    */
    public static long genFileSize(String distFilePath) {
    File distFile = new File(distFilePath);
    if (distFile.isFile()) {
    return distFile.length();
    } else if (distFile.isDirectory()) {
    return FileUtils.sizeOfDirectory(distFile);
    }
    return -1L;
    }
    /**
    * 判斷一個文件是否存在
    *
    * @param filePath 文件路徑
    * @return 存在返回true,否則返回false
    */
    public static boolean isExist(String filePath) {
    return new File(filePath).exists();
    }
    /**
    * 本地某個目錄下的文件列表(不遞歸)
    *
    * @param folder ftp上的某個目錄
    * @param suffix 文件的后綴名(比如.mov.xml)
    * @return 文件名稱列表
    */
    public static String[] listFilebySuffix(String folder, String suffix) {
    IOFileFilter fileFilter1 = new SuffixFileFilter(suffix);
    IOFileFilter fileFilter2 = new NotFileFilter(DirectoryFileFilter.INSTANCE);
    FilenameFilter filenameFilter = new AndFileFilter(fileFilter1, fileFilter2);
    return new File(folder).list(filenameFilter);
    }
    /**
    * 將字符串寫入指定文件(當指定的父路徑中文件夾不存在時,會限度去創(chuàng)建,以保證保存成功!)
    *
    * @param res 原字符串
    * @param filePath 文件路徑
    * @return 成功標記
    */
    public static boolean string2File(String res, String filePath) {
    boolean flag = true;
    BufferedReader bufferedReader = null;
    BufferedWriter bufferedWriter = null;
    try {
    File distFile = new File(filePath);
    if (!distFile.getParentFile().exists()) distFile.getParentFile().mkdirs();
    bufferedReader = new BufferedReader(new StringReader(res));
    bufferedWriter = new BufferedWriter(new FileWriter(distFile));
    char buf[] = new char[1024]; //字符緩沖區(qū)
    int len;
    while ((len = bufferedReader.read(buf)) != -1) {
    bufferedWriter.write(buf, 0, len);
    }
    bufferedWriter.flush();
    bufferedReader.close();
    bufferedWriter.close();
    } catch (IOException e) {
    flag = false;
    e.printStackTrace();
    }
    return flag;
    }
    }