Java复制一个目录下所有的文件夹到另一个目录下详解编程语言

Java复制一个目录下所有的文件夹到另一个目录下

      import java.io.*; 
        import java.util.*; 
        private static File[] copyfoldersList = new File("C://Windows//System32").listFiles(); 
        for (int k = 0; k < copyfoldersList.length; k++) { 
            if (copyfoldersList[k].isDirectory()) { 
                LinkedList<String> copysourcepath = new LinkedList<String>( 
                        Arrays.asList(copyfoldersList[k].getAbsolutePath())); 
                LinkedList<String> copytargetpath = new LinkedList<String>( 
                        Arrays.asList("D:"+ File.separator+ copyfoldersList[k].getAbsolutePath().substring(copyfoldersList[k].getAbsolutePath().lastIndexOf(File.separator)))); 
                while (copysourcepath.size() > 0) { 
                    (new File(copytargetpath.peek())).mkdirs(); 
                    File folders = new File(copysourcepath.peek()); 
                    String[] file = folders.list(); 
                    File temp = null; 
                    for (int i = 0; i < file.length; i++) { 
                        if (copysourcepath.peek().endsWith(File.separator)) 
                            temp = new File(copysourcepath.peek(), file[i]); 
                        else 
                            temp = new File(copysourcepath.peek(), file[i]); 
                        FileInputStream input = null; 
                        FileOutputStream output = null; 
                        if (temp.isFile()) { 
                            try { 
                                input = new FileInputStream(temp); 
                                output = new FileOutputStream(new File(copytargetpath.peek(), temp.getName().toString())); 
                                long filelength=input.length(); 
                                long buffsize=filelength<10485760L?filelength:10485760L; 
                                byte[] b = new byte[buffsize]; 
                                int len; 
                                while ((len = input.read(b)) != -1) 
                                    output.write(b, 0, len); 
                                output.flush(); 
                            } catch (IOException e) { 
                                System.err.println("复制单个文件操作出错"); 
                            } finally { 
                                try { 
                                    output.close(); 
                                    input.close(); 
                                } catch (IOException e) { 
                                } 
                            } 
                        } else if (temp.isDirectory()) { 
                            for (File f : temp.listFiles()) { 
                                if (f.isDirectory()) { 
                                    copysourcepath.add(f.getPath()); 
                                    copytargetpath.add(copytargetpath.peek() 
                                            + File.separator + f.getName()); 
                                } else if (f.isFile()) { 
                                    new File(copytargetpath.peek()+ File.separator+ temp.getName()).mkdirs(); 
                                    FileInputStream input2 = null; 
                                    FileOutputStream output2 = null; 
                                    try { 
                                        input2 = new FileInputStream(f); 
                                        output2 = new FileOutputStream(copytargetpath.peek()+ File.separator+ temp.getName()+ File.separator+ f.getName()); 
                                        long filelength=input2.length(); 
                                        long buffsize=filelength<10485760L?filelength:10485760L; 
                                        byte[] b = new byte[buffsize]; 
                                        int len; 
                                        while ((len = input2.read(b)) != -1) 
                                            output2.write(b, 0, len); 
                                        output2.flush(); 
                                    } catch (IOException e) { 
                                        System.err.println("复制单个文件操作出错"); 
                                    } finally { 
                                        try { 
                                            output2.close(); 
                                            input2.close(); 
                                        } catch (IOException e) { 
                                        } 
                                    } 
                                } 
                            } 
                        } 
                    } 
                    copysourcepath.removeFirst(); 
                    copytargetpath.removeFirst(); 
                } 
            } 
        }

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10852.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论