JAVA复制单级目录及目录下的文件


package Iodemo;

import java.io.*;

/**
 * 复制单级目录
 */
public class CopyOneDir {
    public static void main(String[] args) throws IOException {
        File srcFolder  =new File("/mac/java_test/aa");
        String srcFolderName =  srcFolder.getName();
        File destFolder = new File("./",srcFolderName);
        if (!destFolder.exists()){
            destFolder.mkdir();
        }
        File[] listFiles = srcFolder.listFiles();
        for (File srcFile:listFiles){
            String name = srcFile.getName();
            File destFile = new File(destFolder,name);
            CopyFile(srcFile,destFile);
        }
    }

    private static void CopyFile(File srcFile, File destFile) throws IOException {
        BufferedInputStream bis =new BufferedInputStream(new FileInputStream(srcFile));

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