当前位置:编程学习 > JAVA >>

java遍历文件夹并复制文件到指定目录

package com.czp; 
 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Scanner; 
 
import javax.swing.JTextArea; 
 
public class CopyFolder { 
 
    //允许复制的文件类型  
    public static String[] filterFile = {".java",".xml",".xdl",".properties",".sql",".jupiter",".wsdl"}; 
    private  long total = 0l; 
    //private static Pattern pattern = Pattern.compile("[A-z][:]/[A-z]*/OMC[0-9A-z]{0,}");  
     
    public static void main(String[] args) throws Exception { 
 
        Scanner scanner = new Scanner(System.in); 
        System.out.println("Enter src folder:"); 
        String srcStr = scanner.next(); 
        String destStr; 
        System.out.println("Now enter dest folder:"); 
        destStr = scanner.next(); 
        String answer = null; 
        do{ 
            File src = new File(srcStr); 
            File des = new File(destStr); 
            new CopyFolder().copyFolder(src, des,new String[]{".java",".xml",".xdl",".properties",".sql",".jupiter"},null); 
            System.out.println("Continue ?y or n"); 
            answer = scanner.next(); 
        }while(answer.equalsIgnoreCase("Y")); 
        scanner.close(); 
    } 
 
    /** www.zzzyk.com
     * 
     * @param folder
     * @param filterFile 
     * @param status 
     * @throws Exception 
     */ 
    public void copyFolder(File srcFolder,File destFolder,String[] filterFile, JTextArea status) throws Exception 
    { 
        File[] files = srcFolder.listFiles(); 
        for (File file : files) 
        { 
            if(file.isFile()) 
            { 
                String pathname = destFolder+File.separator+file.getName(); 
                 
                for (String suff : filterFile) 
                { 
                     if(pathname.endsWith(suff)) 
                     { 
                         File dest = new File(pathname); 
                         File destPar =  dest.getParentFile(); 
                         destPar.mkdirs(); 
                         if(!dest.exists()) 
                         { 
                             dest.createNewFile(); 
                         } 
                         copyFile(file, dest,status); 
                     } 
                } 
            }else{ 
                 copyFolder(file, destFolder, filterFile,status); 
            } 
        } 
    } 
    /***
     * copy file
     * 
     * @param src
     * @param dest
     * @param status 
     * @throws IOException
     */ 
    private void copyFile(File src, File dest, JTextArea status) throws Exception { 
        BufferedInputStream reader = null; 
        BufferedOutputStream writer = null; 
        try { 
            reader = new BufferedInputStream(new FileInputStream(src)); 
            writer = new BufferedOutputStream(new FileOutputStream(dest));  
            byte[] buff = new byte[reader.available()]; 
            while ((reader.read(buff)) != -1) { 
                writer.write(buff); 
            } 
            total++; 补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,