8998b81e7f19ad67e41d9784da379a55255515a0.svn-base 2.39 KB
package com.thinkgem.jeesite.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.utils.JsonUtil;

public class FindFileTest {
	
	public static void main(String[] args) {
		String namefile = "G:\\names.txt";
		List<String> names = getNameList(namefile);
		
		String srcpath = "G:\\updatas\\";
		String distpath = "G:\\dicts\\";
		
		Integer existsNum = 0;
		Integer qsNum = 0;
		
		for (String name : names) {
			String filename = "Biz"+name+".xml";
			Boolean exists = exists(srcpath, filename);
			if(exists) { 
				copyFile(srcpath, distpath, filename);
				existsNum++;
			}else { 
				qsNum++;
			}
		}
		
		System.out.println("总数:"+names.size()+",存在:"+existsNum+",缺失:"+qsNum);
		 
	}
	
	
	private static void copyFile( String srcpath,String distpath,String name) { 
		File srcFile = new File(srcpath+name);  
		File distFile = new File(distpath+name);   
		
		InputStream in = null;
	    OutputStream out = null;
	    try {
	        in = new FileInputStream(srcFile);
	        out = new FileOutputStream(distFile);

	        byte[] buffer = new byte[10240];
	        int len;

	        while ((len = in.read(buffer)) > 0) {
	            out.write(buffer, 0, len);
	        }
	        
	        System.out.println("文件"+name+"复制完成");
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	    	try {
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	
	    }
		
	}
	
	private static Boolean exists( String srcpath, String name) {  
		File srcFile = new File(srcpath+name); 
		Boolean exist = srcFile.exists(); 
		System.out.println("文件"+(srcpath+name)+(exist?"存在":"缺失"));
		return exist;
	}
	
	private static List<String> getNameList(String filepath){
		List list = Lists.newArrayList(); 
		try {
			FileReader fr = new FileReader(filepath);
			BufferedReader bf = new BufferedReader(fr);
			String str;
			// 按行读取字符串
			while ((str = bf.readLine()) != null) {
				list.add(str);
			}
			bf.close();
			fr.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		return list;
	}
	
	
}