Java,復(fù)制文件夾及所有子文件,并排重

直接復(fù)制下面代碼即可,可直接使用

public static void moveFile(String orgin_path, String moved_path) {
        File[] files = new File(orgin_path).listFiles();
        for (File file : files) {
            if (file.isFile()) {// 如果是文件
                File movedFile = new File(moved_path + File.separator + file.getName());
                  // 如果文件已經(jīng)存在則跳過(guò)
                if (movedFile.exists()) {
                    System.out.println("文件已經(jīng)存在1:" + file.getAbsolutePath());
                    System.out.println("文件已經(jīng)存在2:" + movedFile.getAbsolutePath());
                    continue;
                } else {
                    // 否則復(fù)制
                    try {
                        FileUtil.copyFile(file, movedFile);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            if (file.isDirectory()) {// 如果是目錄,就遞歸
                String childMPath = moved_path + File.separator + file.getName();
                new File(childMPath).mkdir();
                moveFile(file.getAbsolutePath(), childMPath);
            }
        } 
    }

    public static void copyFile(File resource, File target) throws Exception {
        FileInputStream inputStream = new FileInputStream(resource);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        FileOutputStream outputStream = new FileOutputStream(target);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        byte[] bytes = new byte[1024 * 2];
        int len = 0;
        while ((len = inputStream.read(bytes)) != -1) {
            bufferedOutputStream.write(bytes, 0, len);
        }
        bufferedOutputStream.flush();
        bufferedInputStream.close();
        bufferedOutputStream.close();
        inputStream.close();
        outputStream.close();
        long end = System.currentTimeMillis();
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本篇就一下方面展開分析 如何使用主從復(fù)制? 主從復(fù)制的原理(重點(diǎn)是全量復(fù)制和部分復(fù)制、以及心跳機(jī)制) 實(shí)際應(yīng)用中需...
    lucode閱讀 1,066評(píng)論 0 5
  • feisky云計(jì)算、虛擬化與Linux技術(shù)筆記posts - 1014, comments - 298, trac...
    不排版閱讀 4,356評(píng)論 0 5
  • 太陽(yáng)光,金亮亮。 寶貝上學(xué)校。 小書包,背背好。 媽媽小手牽。 小寶貝,揮揮手。 老師老師早上好。?? 語(yǔ)言智能《...
    season_98a9閱讀 327評(píng)論 0 0
  • 2020年1月16日夜 說(shuō)起冷戰(zhàn),更可以說(shuō)是無(wú)奈,在育兒路上自己已經(jīng)心力憔悴。 忙完一天的工作,本是白天所有的焦灼...
    幽幽白書0閱讀 235評(píng)論 0 1

友情鏈接更多精彩內(nèi)容