1.单级文件复制

/*
 * 需求:将有一个单级目录下的.java 文件复制到另外一个目录中去,并修改后缀名为.txt,如果修改后的文件是已经存在的,删除旧文件
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        // 封装目录
        File srcF = new File("E:\\aaa");
        // 封装destF
        File destF = new File("E:\\copy");
        //如果destF的目录不存在,就创建
        if (!destF.exists()) {
            destF.mkdir();
        }

        //获取源目录的文件下的所有.java 文件,返回file数组
        File[] fArr = srcF.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                //dir   E:\aaa  文件的目录
                //name   E:\Test.java  目录下的文件名
                return new File(dir, name).isFile() && name.endsWith(".java");
            }
        });

        // 遍历该File数组,得到每一个File对象
        for (File file : fArr) {
             //file  E:\aaa\Test.java
            String name = file.getName();
            //name   Test.java
            //拼接目的文件File
            File newFile = new File(destF, name);
            //newFile  E:\copy\Test.java
            fileToFileByBufByteArray(file.toString(), newFile.toString());
        }

        // 在目的地目录下所有文件
        File[] destFileArray = destF.listFiles();
        for (File destFile : destFileArray) {
            //destFile    E:\copy\Test.java
            String name =destFile.getName();
            //name  Test.java
            if(name.endsWith(".java")){
                String destName = name.replace(".java", ".txt");//DataTypeDemo.jad
                //destName    Test.txt
                File newFile = new File(destF,destName);
                //将原先的文件后缀名修改,如果修改后的文件已经存在,无法覆盖,修改后缀名失败
                if(newFile.exists()){
                    //如果目的文件存在,删除,因为renameTo方法修改后的文件存在的话,renameTo失败
                    newFile.delete();
                    destFile.renameTo(newFile);
                }else{
                    destFile.renameTo(newFile);
                }
            }
        }
    }

    //使用字节方法,将一个文件写到另外一个文件中,高效缓存,字节数组读取,建议使用
    public static void fileToFileByBufByteArray(String src,String dest) throws IOException{
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
        byte[] bys=new byte[1024];
        int len=0;
        while((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bos.close();
        bis.close();
    }
}

2.多级文件复制


/*
 * 需求:复制多极文件夹到指定目录

 */
public class Test {
    public static void main(String[] args) throws IOException {
        //将E:\aaa所有文件复制到E:\bbb的目录下
        File srcFile = new File("E:\\aaa");
        File destFile = new File("E:\\bbb");
        copyFolder(srcFile, destFile);
    }

    private static void copyFolder(File srcFile, File destFile)
            throws IOException {
        // 复制目录结构到目的文件区
        if (srcFile.isDirectory()) {
            // 文件夹,拼接目的文件的目录结构
            File newFolder = new File(destFile, srcFile.getName());
                newFolder.mkdirs();//如果目的文件目录存在,创建失败
            // 获取该File对象下的所有文件或者文件夹File对象

            File[] fileArray = srcFile.listFiles();
            //递归操作
            for (File file : fileArray) {
                //子文件,重复copyFolder
                copyFolder(file, newFolder);
            }
        } else {
            //复制文件到目的文件中去
            File newFile = new File(destFile, srcFile.getName());
            fileToFileByBufByteArray(srcFile.toString(), newFile.toString());
        }
    }

    //使用字节方法,将一个文件写到另外一个文件中,高效缓存,字节数组读取,建议使用
    public static void fileToFileByBufByteArray(String src,String dest) throws IOException{
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
        byte[] bys=new byte[1024];
        int len=0;
        while((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bos.close();
        bis.close();
    }
}
posted on 2017-04-08 17:33  2637282556  阅读(163)  评论(0编辑  收藏  举报