java将一个文件的图片(等)复制到另一个文件
public class copyToFile{
private int num = 0;
//将一个文件的东西全部复制到另一个文件夹
    public boolean process() {
        //文件名后面加日期
        //Calendar calendar = Calendar.getInstance();
        //String dir = calendar.get(Calendar.YEAR) + "" + getTimeString(calendar.get(Calendar.MONTH)+ 1 + "");
        
 String savePath = "D://image";
String copyToPath = "D://image_copy//copy"; //String copyToPath = "D://image_copy//copy"+dir;
        try {
            while(true){
                System.out.println("复制 " + savePath + " 目录开始");
                long t1 = System.currentTimeMillis();
                num = 0;
                if(copyFolder(savePath, copyToPath)){
                	long t2 = System.currentTimeMillis();
                    System.out.println("复制目录结束,用时:" + (t2-t1) + "ms,共复制:" + num + "文件");
                	break;
                }else{
                	return false;
                }
            }
        	//}
        	return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        
        }
		return false;
    }
  
    public boolean copyFolder(String savePath, String copyToPath) {
        
        try {
            File mFile = new File(copyToPath);
            if(!mFile .exists()){
                (new File(copyToPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
            }
            File a = new File(savePath);
            String[] file = a.list();
            File temp = null;
            for (int i = 0; i < file.length; i++) {
                if (savePath.endsWith(File.separator)) {
                    temp = new File(savePath + file[i]);
                } else {
                    temp = new File(savePath + File.separator + file[i]);
                }
                if (temp.isFile()) {
                    String fileName = copyToPath + "/" + (temp.getName()).toString();
                    File testFile = new File(fileName);
                    if (!testFile.exists()) {
                        FileInputStream input = new FileInputStream(temp);
                        FileOutputStream output = new FileOutputStream(fileName);
                        byte[] b = new byte[1024 * 5];
                        int len;
                        while ((len = input.read(b)) != -1) {
                            output.write(b, 0, len);
                        }
                        output.flush();
                        output.close();
                        input.close();
                        num++;
                    }
                }
                if (temp.isDirectory()) {// 如果是子文件夹
                    copyFolder(savePath + "/" + file[i], copyToPath + "/" + file[i]);
                }
            }
            return true;
        } catch (Exception e) {
            System.out.println("复制整个文件夹内容操作出错");
            e.printStackTrace();
            return false;
        }
    }
    
    private String getTimeString(String time){
        if(time.length()<2){
            return "0" + time;
        }
        else{
            return time;
        }
    }
}
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号