zoukankan      html  css  js  c++  java
  • IO 文件夹的拷贝

    package FileCopy;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.LinkedList;
    import java.util.Queue;
    
    public class CopyFileV2 {
        public static void main(String agrs[]){
            String srcFileName = "F:" + File.separator + "ppt";
            String destFilePath = "D:" + File.separator + "copyFileExam";
            
    //        copy(srcFileName, destFilePath);
            rNoCopy(srcFileName, destFilePath);
            System.out.println("拷贝完成!!!");
            
        }
        
        private static void rNoCopy(String fileName, String destFileName){
            
            Queue<String[]> fPQ = new LinkedList<String[]>();
            String[] s = new String[2];
            s[0] = fileName;
            s[1] = destFileName;
            fPQ.offer(s);
            
            int i = 0;
            
            while(!fPQ.isEmpty()){
                
                for(String[] st : fPQ){
                    for(String str : st)
                    System.out.println(str);
                }
                
                System.out.println("
    " + i++);
                String[] filePath = fPQ.poll();
                
                File file = new File(filePath[0]);
                File destFile = new File(filePath[1]);
                
                if(!destFile.exists()){
                    destFile.mkdirs();
                    System.out.println("创建多级列表成功!!!");
                }
                
                File files[] = file.listFiles();
                
                for(File f: files){
                    if(f.isFile()){
                        fileCopy(f.getPath(), filePath[1] + File.separator + f.getName());
                    }
                    else if(f.isDirectory()){
                        String[] temps = new String[2]; 
                        
                        temps[0] = f.getPath();
                        temps[1] = filePath[1] + File.separator + f.getName();
                        
                        fPQ.offer(temps);
                    }
                }
            }
        }
    
        private static void copy(String fileName, String destFileName){
            File file = new File(fileName);
            File destFile = new File(destFileName);
            
            File files[] = file.listFiles();
            
            if(!destFile.exists()){
                destFile.mkdirs();
            }
            
            for(File f: files){
                if(f.isFile()){
                    fileCopy(f.getPath(), destFileName + File.separator + f.getName());
                }
                else if(f.isDirectory()){
                    copy(f.getPath(), destFileName + File.separator + f.getName());
                }
            }
            
        }
        
        private static void fileCopy(String fileName, String destFileName){
            System.out.println("正在拷贝文件!!!");
            File file = new File(fileName);
            File destFile = new File(destFileName);
            
    //        InputStream is = null;
    //        OutputStream os = null;
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
    
            try {
                bis = new BufferedInputStream(new FileInputStream(file));
                bos = new BufferedOutputStream(new FileOutputStream(destFile));
                
                byte bytes[] = new byte[1024];
                while( bis.read(bytes) != -1 ){
                    bos.write(bytes);
                    bos.flush();
                }
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
    //                os.flush();
    //                is.close();
    //                os.close();
                    bos.flush();
                    bis.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
    //        private static void getAllFileList(String fileName){
    //            File file = new File(fileName);
    //            File files[] = file.listFiles();
    //            
    //            for(File f: files){
    //                if(f.isFile()){
    //                    
    //                }
    //                if(f.isDirectory()){
    //                    getAllFileList(f.toString());
    //                }
    //                else {
    //                    System.out.println(f.getName());
    //                }
    //            }
    //        
    //    }
        }
    }
  • 相关阅读:
    leetcode-594-Longest Harmonious Subsequence
    leetcode-581-Shortest Unsorted Continuous Subarray
    leetcode-575-Distribute Candies(计算一个数组中元素的种类的快速方法)
    leetcode-566-Reshape the Matrix
    leetcode-561-Array Partition I
    leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)
    Java之CheckedException
    Java异常机制
    Zookeeper分布式协调即分布式锁机制
    Spring中@Value用法
  • 原文地址:https://www.cnblogs.com/854594834-YT/p/10527471.html
Copyright © 2011-2022 走看看