java循环更新文件夹

1.创建Java项目,编写替换类文件:
package com.tp.update;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class UpdateFile {
	private static String source;
	private static String target;

	public static void main(String[] args) {
        source = args[0];
        target = args[1];
        getFile(source);
	}
	
	public static void getFile(String list_path){
		File root = new File(list_path);
        File[] files = root.listFiles();
        
        for(int i=0;i<files.length;i++){
        	String path = files[i].getPath();
        	String child_path = path.substring(source.length());
    		String target_child_path = target+child_path;
    		
        	if(files[i].isDirectory()){
        		//首先判断该文件夹在目标路径中是否已经存在
        		File f = new File(target_child_path);
        		if(f.exists()){
        			getFile(path);
        		}else{
        			f.mkdirs();
        			getFile(path);
        		}
        		
        	}else{
        		//String child_path = path.substring(source.length());
        		//String target_child_path = target+child_path;
        		File target_child_file = new File(target_child_path);
        		if (target_child_file.exists()){
        			long c_date = target_child_file.lastModified();
        			String t_folder_name = target_child_file.getPath();
        			t_folder_name  =t_folder_name.substring(0,t_folder_name.lastIndexOf("\\"));
        			String t_file_name  = target_child_file.getName();
        			target_child_file.renameTo(new File(t_folder_name+"\\"+t_file_name+"."+c_date));
        		}
        		
        		copyFile(path,target_child_path);
        	}
        }
	}
        /** 
         * 复制单个文件 
         * @param oldPath String 原文件路径 如:c:/fqf.txt 
         * @param newPath String 复制后路径 如:f:/fqf.txt 
         * @return boolean 
         */ 
       public static 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(); 

           } 
       } 

}
 

2.右键项目----export----java----runnable jar file----打成jar包 updateProject.jar

3.编写启动项 start.bat,并和jar放在同一路径下

4.启动bat

附bat文件内容:

@echo off
D:\JDK\jdk1.7.0_60\bin\java.exe(指定jdk) -jar updateProject.jar F:\test\ceb1.war(替换文件路径) F:\test\ceb.war(被替换文件路径)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值