亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制

 更新時(shí)間:2021年08月10日 15:16:20   作者:Jae_Du  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制的具體代碼,供大家參考,具體內(nèi)容如下

package com.jae;

import java.io.*;

//復(fù)制文件夾內(nèi)的內(nèi)容,包含多級(jí)文件夾
public class Test2 {
    public static void main(String[] args) throws Exception {
        //原文件夾地址
        File resPath = new File("E:\\Java\\分享");
        File destPath = new File("E:\\");
        //method(resPath, destPath);
        copy(resPath, destPath);
    }
    public static void copy(File src,File dest) throws Exception {
        File newDir = new File(dest,src.getName());
        newDir.mkdir();
        File[] subFiles = src.listFiles();
        for (File subFile : subFiles) {
            if(subFile.isFile()){
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
                BufferedOutputStream bos =
                        new BufferedOutputStream(
                                new FileOutputStream(
                                        new File(newDir,subFile.getName())));
                int b;
                byte[] bytes = new byte[1024 * 8];
                while((b = bis.read(bytes)) != -1){
                    bos.write(bytes,0,b);
                }
                bis.close();
                bos.close();
            }else{
                copy(subFile,newDir);//遞歸調(diào)用
            }

        }
    }
    public static void method(File dir, File destPath) throws IOException {
        //獲取源路徑的文件
        File[] files = dir.listFiles();
        //根目錄文件夾名
        String name1 = dir.getName();
        //遍歷源路徑的文件
        for (File file : files) {
            if (file.isFile()) {

                StringBuilder sb = new StringBuilder(destPath.getAbsolutePath());
                sb.append("\\").append(file.getName());
                //獲取盤(pán)符后面的路徑和文件名
                //String s = absolutePath.split(":")[1];

                //創(chuàng)建輸入流,封裝獲取到的文件的絕對(duì)路徑
                FileInputStream fis = new FileInputStream(file.getAbsolutePath());
                //目標(biāo)路徑,定義目標(biāo)路徑的盤(pán)符,和要復(fù)制的文件路徑和文件名
                //FileOutputStream fos = new FileOutputStream("F:" + s);
                FileOutputStream fos = new FileOutputStream(sb.toString());

                //復(fù)制文件操作
                int len;
                byte[] bytes = new byte[1024 * 8];
                while ((len = fis.read(bytes)) != -1) {
                    fos.write(bytes, 0, len);
                    fos.flush();
                }

                fos.close();
                fis.close();
            } else {

                StringBuilder sb = new StringBuilder(destPath.getAbsolutePath());
                sb.append("\\").append(name1).append("\\").append(file.getName());

                /*String name1 = file.getName();
                //獲取文件夾的路徑
                String name = file.getPath();
                //獲取盤(pán)符:后的文件夾路徑
                String s = name.split(":")[1];*/
                //創(chuàng)建文件夾路徑
                //File file1 = new File("F:" + name1);
                File file1 = new File(sb.toString());
                file1.mkdirs();
                //System.out.println(name);
                method(file, file1);
            }
        }
    }
}

再為大家補(bǔ)充一段代碼:Java遞歸復(fù)制多級(jí)目錄及文件,感謝原作者的分享

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:復(fù)制多級(jí)文件夾
 * 
 * 數(shù)據(jù)源:D:\\1
 * 目的地:F:\\新建文件夾
 * 
 * 分析:
 *      A:封裝數(shù)據(jù)源File
 *      B:封裝目的地File
 *      C:判斷該File是文件夾還是文件
 *          a:是文件夾
 *              就在目的地目錄下創(chuàng)建該文件夾
 *              獲取該File對(duì)象下的所有文件或者文件夾File對(duì)象
 *              遍歷得到每一個(gè)File對(duì)象
 *              回到C
 *          b:是文件
 *              就復(fù)制(字節(jié)流)
 */


public class C1 {
public static void main(String[] args) throws IOException {
    File srcFolder = new File("D:\\1");
    File dstFolder = new File("F:\\新建文件夾");
    judge(srcFolder,dstFolder);
}

private static void judge(File srcFolder,File dstFolder) throws IOException {
    if(srcFolder.isDirectory()){
        File newFolder = new File(dstFolder,srcFolder.getName());
        newFolder.mkdir();

        File[] fileArr = srcFolder.listFiles();
        for(File f:fileArr){
            judge(f, newFolder);

        }
    }else{
        File newFile = new File(dstFolder,srcFolder.getName());
//      System.out.println(newFile);
        copyFile(srcFolder,newFile);

    }

}

private static void copyFile(File srcFolder, File newFile) throws IOException {
    // TODO Auto-generated method stub

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
            srcFolder));
    BufferedOutputStream bos = new BufferedOutputStream(
            new FileOutputStream(newFile));

    byte[] bys = new byte[1024];
    int len = 0;
    while ((len = bis.read(bys)) != -1) {
        bos.write(bys, 0, len);

    }

    bos.close();
    bis.close();
}


}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論