import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
 
public class CopyFile { 
	 
	/**  
     * 复制单个文件  
     * @param oldPath String 原文件路径 如:c:/fqf.txt  
     * @param newPath String 复制后路径 如:f:/fqf.txt  
     * @return boolean  
     */  
   public void copyFile(String oldPath, String newPath) {  
       try {  
           int bytesum = 0;  
           int byteread = 0;  
           File oldfile = new File(oldPath);  
           if (oldfile.exists()) { //文件存在时  
               InputStream inStream = new FileInputStream(oldPath); //读入原文件  
               FileOutputStream fs = new FileOutputStream(newPath);  
               byte[] buffer = new byte[1444];  
               int length;  
               while ( (byteread = inStream.read(buffer)) != -1) {  
                   bytesum += byteread; //字节数 文件大小  
                   System.out.println(bytesum);  
                   fs.write(buffer, 0, byteread);  
               }  
               inStream.close();  
           }  
       }  
       catch (Exception e) {  
           System.out.println("复制单个文件操作出错");  
           e.printStackTrace();  
 
       }  
 
   }  
 
}


import java.io.File;  
import java.util.ArrayList;  
import java.util.Iterator;  
import java.util.List;  
 
public class FileViewer { 
 
    /** 
     * 读取文件夹下所以文件 
     * 
     * @param dirPath 
     * @param fileTypes 
     * @return 
     */ 
    public static List<String> getAllFilePathByDir_FilterByFileType(String dirPath, 
            List<String> fileTypes,String destination) { 
 
        String localClassFilePath = FileViewer.class.getResource("/").getPath(); 
        String contextFilePath = localClassFilePath.substring(1, localClassFilePath.length() - 16); 
        contextFilePath = contextFilePath.replace("/", "\\"); 
 
        List<String> filePathList = new ArrayList<String>(); 
        for (int index = 0; index < fileTypes.size(); index++) { 
            fileList = new ArrayList<String>(); 
            List arrayList = FileViewer.getListFiles(dirPath, fileTypes.get(index), true); 
            if (arrayList.isEmpty()) { 
                System.out.println("没有符号要求的文件"); 
            } else { 
                for (Iterator i = arrayList.iterator(); i.hasNext();) { 
                    String temp = (String) i.next(); 
                    temp = temp.replace(contextFilePath, "");//把根路径去掉  
                     
                    System.out.println(temp); 
                	CopyFile cp=new CopyFile(); 
            		cp.copyFile(temp, destination+Util.GetReName(temp)); 
                     
                    filePathList.add(temp); 
                } 
            } 
        } 
        for (int replaceIndex = 0; replaceIndex < filePathList.size(); replaceIndex++) { 
            filePathList.set(replaceIndex, filePathList.get(replaceIndex).replace("\\", "/")); 
        } 
        return filePathList; 
    } 
    public static List<String> fileList = new ArrayList<String>(); 
 
    /** 
     * 添加文件后缀名称 
     * 
     * @return 
     */ 
    public static List<String> suffixlist(String suffix) { 
        List<String> list = new ArrayList<String>(); 
        String[] types=suffix.split(","); 
        for(int i=0;i<types.length;i++) 
        { 
        	list.add(types[i]); 
        } 
        return list; 
    } 
 
    /** 
     * 
     * @param path 文件路径 
     * @param suffix 后缀名 
     * @param isdepth 是否遍历子目录 
     * @return 
     */ 
    public static List getListFiles(String path, String suffix, boolean isdepth) { 
        File file = new File(path); 
        return FileViewer.listFile(file, suffix, isdepth); 
    } 
 
    public static List listFile(File f, String suffix, boolean isdepth) { 
        // 是目录,同时需要遍历子目录  
        if (f.isDirectory() && isdepth == true) { 
            File[] t = f.listFiles(); 
            for (int i = 0; i < t.length; i++) { 
                listFile(t[i], suffix, isdepth); 
            } 
        } else { 
            String filePath = f.getAbsolutePath(); 
            if (suffix != null) { 
                int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引  
                String tempsuffix = ""; 
 
                if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件  
                { 
                    tempsuffix = filePath.substring(begIndex + 1, filePath 
                            .length()); 
                } 
                if (tempsuffix.equals(suffix)) { 
                    fileList.add(filePath); 
                } 
            } else { 
                // 后缀名为null则为所有文件  
                fileList.add(filePath); 
            } 
        } 
        return fileList; 
    } 
 
}


import java.util.List; 
 
 
public class Test { 
 
	public static void main(String[] args) { 
		 
		 List<String> listResult = FileViewer.getAllFilePathByDir_FilterByFileType("D:\\lib", FileViewer.suffixlist("jar"),"D:\\cordys\\"); 
	       
	    } 
	 
	 
	 
 
}






发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Oracle客户端 无法监听设置知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。