最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實現(xiàn)文件復(fù)制、剪切文件和刪除示例

 更新時間:2014年04月16日 09:55:24   作者:  
這篇文章主要介紹了java實現(xiàn)文件復(fù)制、剪切文件和刪除示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Java實現(xiàn)文件復(fù)制、剪切、刪除操作
 * 文件指文件或文件夾
 * 文件分割符統(tǒng)一用"\\"
 */

public class FileOperateDemo {

    /**
     * 復(fù)制文件或文件夾
     * @param srcPath 源文件或源文件夾的路徑
     * @param destDir 目標(biāo)文件所在的目錄
     * @return
     */
    public static boolean copyGeneralFile(String srcPath, String destDir) {
        boolean flag = false;
        File file = new File(srcPath);
        if(!file.exists()) { // 源文件或源文件夾不存在
            return false;
        }

        if(file.isFile()) {    // 文件復(fù)制
            flag = copyFile(srcPath, destDir);
        }
        else if(file.isDirectory()) { // 文件夾復(fù)制
            flag = copyDirectory(srcPath, destDir);
        }

        return flag;
    }

    /**
     * 默認的復(fù)制文件方法,默認會覆蓋目標(biāo)文件夾下的同名文件
     * @param srcPath
     *            源文件絕對路徑
     * @param destDir
     *            目標(biāo)文件所在目錄
     * @return boolean
     */
    public static boolean copyFile(String srcPath, String destDir) {
     return copyFile(srcPath, destDir, true/**overwriteExistFile*/); // 默認覆蓋同名文件
    }

    /**
     * 默認的復(fù)制文件夾方法,默認會覆蓋目標(biāo)文件夾下的同名文件夾
     * @param srcPath    源文件夾路徑
     * @param destPath    目標(biāo)文件夾所在目錄
     * @return boolean
     */
    public static boolean copyDirectory(String srcPath, String destDir) {
     return copyDirectory(srcPath, destDir, true/**overwriteExistDir*/);
    }

    /**
     * 復(fù)制文件到目標(biāo)目錄
     * @param srcPath
     *            源文件絕對路徑
     * @param destDir
     *            目標(biāo)文件所在目錄
     * @param overwriteExistFile
     *            是否覆蓋目標(biāo)目錄下的同名文件
     * @return boolean
     */
    public static boolean copyFile(String srcPath, String destDir, boolean overwriteExistFile) {
        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isFile()) { // 源文件不存在
            return false;
        }

        //獲取待復(fù)制文件的文件名
        String fileName = srcFile.getName();
        String destPath = destDir + File.separator +fileName;
        File destFile = new File(destPath);
        if (destFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) { // 源文件路徑和目標(biāo)文件路徑重復(fù)
            return false;
        }
        if(destFile.exists() && !overwriteExistFile) {    // 目標(biāo)目錄下已有同名文件且不允許覆蓋
            return false;
        }

        File destFileDir = new File(destDir);
        if(!destFileDir.exists() && !destFileDir.mkdirs()) { // 目錄不存在并且創(chuàng)建目錄失敗直接返回
         return false;
        }

        try {
            FileInputStream fis = new FileInputStream(srcPath);
            FileOutputStream fos = new FileOutputStream(destFile);
            byte[] buf = new byte[1024];
            int c;
            while ((c = fis.read(buf)) != -1) {
                fos.write(buf, 0, c);
            }
            fos.flush();
            fis.close();
            fos.close();

            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return flag;
    }

    /**
     *
     * @param srcPath    源文件夾路徑
     * @param destPath    目標(biāo)文件夾所在目錄
     * @return
     */
    public static boolean copyDirectory(String srcPath, String destDir, boolean overwriteExistDir) {
        if(destDir.contains(srcPath))
           return false;

        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isDirectory()) { // 源文件夾不存在
            return false;
        }

        //獲得待復(fù)制的文件夾的名字,比如待復(fù)制的文件夾為"E:\\dir\\"則獲取的名字為"dir"
        String dirName = srcFile.getName();

        //目標(biāo)文件夾的完整路徑
        String destDirPath = destDir + File.separator + dirName + File.separator;
        File destDirFile = new File(destDirPath);
        if(destDirFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) {
         return false;
        }
        if(destDirFile.exists() && destDirFile.isDirectory() && !overwriteExistDir) {    // 目標(biāo)位置有一個同名文件夾且不允許覆蓋同名文件夾,則直接返回false
            return false;
        }

        if(!destDirFile.exists() && !destDirFile.mkdirs()) {  // 如果目標(biāo)目錄不存在并且創(chuàng)建目錄失敗
         return false;
        }

        File[] fileList = srcFile.listFiles();    //獲取源文件夾下的子文件和子文件夾
        if(fileList.length==0) {    // 如果源文件夾為空目錄則直接設(shè)置flag為true,這一步非常隱蔽,debug了很久
            flag = true;
        }
        else {
            for(File temp: fileList) {
                if(temp.isFile()) {    // 文件
                    flag = copyFile(temp.getAbsolutePath(), destDirPath, overwriteExistDir);     // 遞歸復(fù)制時也繼承覆蓋屬性
                }
                else if(temp.isDirectory()) {    // 文件夾
                    flag = copyDirectory(temp.getAbsolutePath(), destDirPath, overwriteExistDir);   // 遞歸復(fù)制時也繼承覆蓋屬性
                }

                if(!flag) {
                    break;
                }
            }
        }

        return flag;
    }

    /**
     * 刪除文件或文件夾
     * @param path
     *            待刪除的文件的絕對路徑
     * @return boolean
     */
    public static boolean deleteFile(String path) {
        boolean flag = false;

        File file = new File(path);
        if (!file.exists()) { // 文件不存在直接返回
            return flag;
        }

        flag = file.delete();

        return flag;
    }

   
    /**
     * 由上面方法延伸出剪切方法:復(fù)制+刪除
     * @param  destDir 同上
     */
    public static boolean cutGeneralFile(String srcPath, String destDir) {
     boolean flag = false;
        if(copyGeneralFile(srcPath, destDir) && deleteFile(srcPath)) { // 復(fù)制和刪除都成功
         flag = true;
        }

        return flag;
    }

    public static void main(String[] args) {
     /** 測試復(fù)制文件 */
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  // 一般正常場景
     System.out.println(copyGeneralFile("d://notexistfile", "d://test/"));      // 復(fù)制不存在的文件或文件夾
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/"));      // 待復(fù)制文件與目標(biāo)文件在同一目錄下
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  // 覆蓋目標(biāo)目錄下的同名文件
     System.out.println(copyFile("d://test/", "d://test2", false)); // 不覆蓋目標(biāo)目錄下的同名文件
     System.out.println(copyGeneralFile("d://test/test.html", "notexist://noexistdir/")); // 復(fù)制文件到一個不可能存在也不可能創(chuàng)建的目錄下

     System.out.println("---------");

     /** 測試復(fù)制文件夾 */
     System.out.println(copyGeneralFile("d://test/", "d://test2/"));

     System.out.println("---------");

     /** 測試刪除文件 */
     System.out.println(deleteFile("d://a/"));
    }

}

相關(guān)文章

  • SpringBoot整合jasypt加密配置文件敏感信息

    SpringBoot整合jasypt加密配置文件敏感信息

    在項目中我們需要對配置文件的一些敏感信息進行加密處理,比如數(shù)據(jù)庫賬戶密碼,避免直接暴露出來,這種場景常常用于生產(chǎn)環(huán)境,我們不想讓開發(fā)人員知道生產(chǎn)庫的密碼,有運維人員統(tǒng)一管理,所以本文給大家介紹了SpringBoot整合jasypt加密配置文件敏感信息
    2024-06-06
  • java中@Configuration使用場景

    java中@Configuration使用場景

    本文主要介紹了java中@Configuration使用場景,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • java.net.SocketTimeoutException: Read timed out異常的解決

    java.net.SocketTimeoutException: Read timed o

    本文主要介紹了java.net.SocketTimeoutException: Read timed out異常的解決,可能是因為網(wǎng)絡(luò)延遲、服務(wù)器響應(yīng)慢或連接不穩(wěn)定等原因造成的,下面就一起來介紹一下,感興趣的可以了解一下
    2024-05-05
  • MyBatis核心源碼深度剖析SQL語句執(zhí)行過程

    MyBatis核心源碼深度剖析SQL語句執(zhí)行過程

    這篇文章主要介紹了MyBatis核心源碼深度剖析SQL執(zhí)行過程,mybatis執(zhí)行SQL的流程都是根據(jù)statement字符串從configuration中獲取對應(yīng)的mappedStatement,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2022-05-05
  • Java常用字符串工具類 字符串智能截?。?)

    Java常用字符串工具類 字符串智能截?。?)

    這篇文章主要為大家詳細介紹了Java常用字符串工具類,字符串的智能截取,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • MyBatis中有關(guān)int和Integer的使用方式

    MyBatis中有關(guān)int和Integer的使用方式

    這篇文章主要介紹了MyBatis中有關(guān)int和Integer的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • feignclient?https?接口調(diào)用報證書錯誤的解決方案

    feignclient?https?接口調(diào)用報證書錯誤的解決方案

    這篇文章主要介紹了feignclient?https?接口調(diào)用報證書錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • RequestContextHolder.getRequestAttributes()空指針問題及解決

    RequestContextHolder.getRequestAttributes()空指針問題及解決

    這篇文章主要介紹了RequestContextHolder.getRequestAttributes()空指針問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Spring單元測試類ApplicationTests錯誤的解決

    Spring單元測試類ApplicationTests錯誤的解決

    這篇文章主要介紹了Spring單元測試類ApplicationTests錯誤的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • spring security集成cas實現(xiàn)單點登錄過程

    spring security集成cas實現(xiàn)單點登錄過程

    這篇文章主要介紹了spring security集成cas實現(xiàn)單點登錄過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02

最新評論

彰化县| 搜索| 加查县| 兴业县| 吐鲁番市| 九江县| 申扎县| 任丘市| 油尖旺区| 沛县| 普定县| 枝江市| 浦北县| 武清区| 淅川县| 抚远县| 新乡市| 平武县| 巨野县| 岳阳县| 邹平县| 即墨市| 永城市| 九江市| 长葛市| 荥阳市| 南投市| 体育| 马公市| 门头沟区| 汝阳县| 石林| 聂拉木县| 革吉县| 东至县| 密云县| 仙游县| 淅川县| 磴口县| 莆田市| 杨浦区|