java循环建立文件夹和文件_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) {

//path1 test1.war

//path2 test.war

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

String path = files[i].getPath();

if(files[i].isDirectory()){

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);

}

try {

System.out.println(files[i].getAbsolutePath());

String t_folder_name = files[i].getPath();

System.out.println(t_folder_name.lastIndexOf("/"));

String f =t_folder_name.substring(0,t_folder_name.lastIndexOf("/"));

System.out.println(f);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

/**

* 复制单个文件

* @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();

}

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值