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());
    //                }
    //            }
    //        
    //    }
        }
    }
  • 相关阅读:
    C#读取EXCEL中数字无法读取的方法
    学习Wml
    sqlserver.exe进程占cpu100%
    windows phone mango 页面跳转事件顺序
    Windows 8 系列(一):win 8 简介
    windows phone 小应用与随机算法的感悟
    文件下载
    html 表格固定宽度
    excel 导入科学计数法
    word 排版
  • 原文地址:https://www.cnblogs.com/854594834-YT/p/10527471.html
Copyright © 2011-2022 走看看