zoukankan      html  css  js  c++  java
  • Java复制目录/子目录/文件

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.channels.FileChannel;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 文件拷贝
     * @author lixiaolong
     */
    public class FileTransfer {
        
        /**
         * 使用通道的方式进行整个目录/子目录/文件的拷贝
         * @throws IOException
         */
        public void useChannel() throws IOException {
            String driver = "F:\AgileController";
            String path = "/tomcat/webapps/OPMUI/customize";
            File input = new File(driver + path);
            
            String bakpath = "\syncbak";
            File bakFile = new File(driver + bakpath);
            if(bakFile.exists())
            {
                deleteFile(bakFile);
            }
            bakFile.mkdirs();
            
            File output = new File(driver + bakpath + path);
            
            if(input.isDirectory()) {
                output.mkdirs();
                
                List<File> allFileList = new ArrayList<File>();
                getAllFiles(input, allFileList);
                for(File f : allFileList) {
                    String outputPath = f.getCanonicalPath();
                    if(outputPath.startsWith(driver))
                    {
                        outputPath = driver + bakpath + outputPath.substring(driver.length(), outputPath.length());
                    }
                    output = new File(outputPath);
                    if(f.isDirectory())
                    {
                        output.mkdirs();
                    } else {
                        fileCopy(f, output);
                    }
                }
            } else {
                fileCopy(input, output);
            }
        }
        
        /**
         * 递归列出所有子目录/文件
         * @param directory
         * @param allFileList
         */
        private void getAllFiles(File directory, List<File> allFileList) {
            File flist[] = directory.listFiles();
            if (flist == null || flist.length == 0) {
                return;
            }
            for (File f : flist) {
                if (f.isDirectory()) {
                    //列出所有子文件夹
                    allFileList.add(f);
                    getAllFiles(f, allFileList);
                } else {
                    //列出所有文件
                    allFileList.add(f);
                }
            }
        }
        
        /**
         * 使用通道的方式对单个文件进行拷贝
         * @param input
         * @param output
         * @throws IOException
         */
        private void fileCopy(File input, File output) throws IOException {
            if(!input.exists()) {
                return;
            }
            
            if(!output.exists()) {
                output.createNewFile();
            }
            
            FileInputStream fis = new FileInputStream(input);
            FileOutputStream fos = new FileOutputStream(output);
            FileChannel inputChannel = null;
            FileChannel outputChannel = null;
            try {
                inputChannel = fis.getChannel();
                outputChannel = fos.getChannel();
                outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
            } finally {
                inputChannel.close();
                outputChannel.close();
                fis.close();
                fos.close();
            }
        }
        
        /**
         * 级联删除文件
         * @param file
         */
        private void deleteFile(File file)
        {
            if (file.isDirectory())
            {
                File[] files = file.listFiles();
                for (File f : files)
                {
                    f.delete();
                }
            }
            file.delete();
        }
        
        public static void main(String[] args) {
            FileTransfer ft = new FileTransfer();
            try {
                ft.useChannel();
            } catch (IOException e) {
                System.out.println(e);
            }
            System.out.println("end");
        }
    
    }
  • 相关阅读:
    【转】Java8学习笔记(1) -- 从函数式接口说起
    解决sublime text 2总是在新窗口中打开文件(标签中打开)
    Cocoapod安装使用
    使用工具来提升Android开发效率
    Material Design之FloatingActionButton的使用
    HDU2842-Chinese Rings(递推+矩阵高速幂)
    阿里笔试题(2015)持续更新中
    Newton‘ method 的优缺点
    解决的方法:warning: Clock skew detected. Your build may be incomplete.
    云享 值得一用的在线文档即时通讯平台 新用户持续免费
  • 原文地址:https://www.cnblogs.com/aaroncnblogs/p/8656180.html
Copyright © 2011-2022 走看看