Java文件操作工具類

jopen 9年前發布 | 3K 次閱讀 Java

移動目錄或者文件, 將一個文件夾下所有文件移動到另一個文件夾下,將指定文件移動到目的目錄,刪除指定目錄和其下所有子目錄和文件,如果指定的是文件則直接刪除

import java.io.File;

/**

  • @Title : FileUtils
  • @File Name : FileUtils.java
  • @Description : 文件操作工具類
  • @Date : 2014年8月10日
  • @author : 王鴻運
  • @version : 1.0
  • @Others :
  • @History 1.<br/>
  • Date : <br/>
  • Author : <br/>
  • Modification: <br/> */ public class FileUtils {

    public static final String FILE_SEP = System.getProperty("file.separator");

    /**

    • @Function : moveFile
    • @Desc : 移動目錄或者文件
    • @Author : 王鴻運
    • @param srcFilePath 源文件(目錄)路徑
    • @param destFilePath 目的文件(目錄)路徑
    • @return true:操作成功,false:操作失敗 */ public static boolean moveFile(String srcFilePath, String destFilePath) { File srcFile = new File(srcFilePath); if (!srcFile.exists()) { return false; }

    if (srcFile.isDirectory()) { File[] subFiles = srcFile.listFiles(); for (File file : subFiles) {

    moveFile(file.getAbsolutePath(), destFilePath +FILE_SEP+ file.getName());
    

    } }

    File destFile = new File(destFilePath); File destParentFile = destFile.getParentFile(); if (!destParentFile.exists()) { destParentFile.mkdirs(); } srcFile.renameTo(destFile);

    return true; }

    /**

    • @Function : moveChildren
    • @Desc : 指定目錄下所有的子目錄和文件到目的目錄
    • @Author : 王鴻運
    • @param srcFilePath 源目錄路徑
    • @param destFilePath 目的路徑
    • @return true:操作成功,false:操作失敗 */ public static boolean moveAllChildren(String srcFilePath, String destFilePath) { File srcDir = new File(srcFilePath); if (!srcDir.exists() || !srcDir.isDirectory()) { return false; }

    File destDir = new File(destFilePath); if (destDir.exists()) { if ( destDir.isFile()) {

    destDir.delete();
    destDir = new File(destFilePath);
    destDir.mkdir();
    

    } } else { destDir.mkdirs(); }

    File[] children = srcDir.listFiles(); for (File file : children) { moveFile(file.getAbsolutePath(), destFilePath); }

    return true; }

    /**

    • @Function : moveFiles
    • @Desc : 將指定文件移動到目的目錄
    • @Author : 王鴻運
    • @param srcFiles 源文件,支持多個路徑參數
    • @param destDirPath 目的目錄
    • @return true:操作成功,false:操作失敗 */ public static boolean moveFiles(String destDirPath, String ... srcFiles){ if (srcFiles == null || srcFiles.length == 0) { return false; } File destDir = new File(destDirPath);

    boolean destExists = true; if (!destDir.exists()) { destExists = destDir.mkdirs(); } else if(destDir.isFile()){ return false; }

    if (!destExists) { return false; }

    for (String srcFilePath : srcFiles) { File srcFile = new File(srcFilePath); if (srcFile.exists() && srcFile.isFile()) {

    srcFile.renameTo(new File(destDirPath + FILE_SEP + srcFile.getName()));
    

    } } return true; }

    /**

    • @Function : deleteSelfAndAllChildren
    • @Desc : 刪除指定目錄和其下所有子目錄和文件,如果指定的是文件則直接刪除
    • @Author : 王鴻運
    • @param dirPath 源目錄或文件路徑
    • @return true:刪除成功,false:刪除失敗 */ public static boolean deleteSelfAndAllChildren(String dirPath) { File srcFile = new File(dirPath); if (!srcFile.exists()) { return false; }

    if (srcFile.isFile()) { srcFile.delete(); } else { File[] children = srcFile.listFiles();

    if (children.length == 0) {

    srcFile.delete();
    

    } else {

    for (File file : children) {//先刪除子文件和目錄
        deleteSelfAndAllChildren(file.getAbsolutePath());
    }
    //再刪除本身
    srcFile.delete();
    

    } } return true; }

    /**

    • @Function : deleteAllChildren
    • @Desc : 刪除一個目錄下所有的子目錄和文件
    • @Author : 王鴻運
    • @param dirPath
    • @return true:刪除成功,false:刪除失敗 */ public static boolean deleteAllChildren(String dirPath){ File dir = new File(dirPath); if (!dir.exists()) { return false; } File[] children = dir.listFiles(); for (File file : children) { deleteSelfAndAllChildren(file.getAbsolutePath()); }

    return true; } }</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!