java 代替递归_Java 递归调用和非递归调用

packagecom.message;importjava.io.File;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.List;importorg.apache.commons.io.FileUtils;public classMessageTest {public static void main(String[] args) throwsException{//String sPath = "E:/message_mm1/message_mm2/";//源目录//String toPath = "E:/message_mm1/message_mm3/";//目标目录//linux 目录//String sPath = "/usr/local/mm_test/";//源目录//String toPath = "/usr/local/mm_test_20170418/";//目标目录

String sPath = "/usr/local/message_mm/message_mm_20170416/";//源目录

String toPath = "/usr/local/messageTest/mm/";//目标目录

List sourceList =getFileSort(sPath);

System.out.println("源目录文件总数========="+sourceList.size());int sIndex = 0; //源目录文件列表开始下标

loopCopy(sIndex,sPath, toPath,sourceList);

}/*** do while循环的方式copy文件

*@paramsIndex 读取源目录文件的下标

*@paramsPath 源目录

*@paramtoPath 目标目录

*@paramsourceList 源目录下文件组成的List*/

public static void loopCopy(int sIndex,String sPath, String toPath,ListsourceList){try{boolean flag = true;do{

File toFile=newFile(toPath);

File[] toList=toFile.listFiles();if(toList.length==0){if(sIndex

FileUtils.copyFile(new File(sPath+sourceList.get(sIndex).getName()), new File(toPath+sourceList.get(sIndex).getName()));System.out.println("拷贝成功,文件名:"+sourceList.get(sIndex).getName()+"文件最后修改时间:"+sourceList.get(sIndex).lastModified());

Thread.sleep(50); //休眠50ms

sIndex++;

}else{

System.out.println("执行结束,共拷贝文件总数="+sIndex);

flag= false;

}

}else{//目标目录文件未处理完,再休眠50ms

Thread.sleep(50);

}

}while(flag);

}catch(Exception e) {

e.printStackTrace();

System.out.println("文件拷贝异常,异常原因:"+e.getMessage());

}

}/*** 递归的方式copy文件

*@paramsIndex 读取源目录文件的下标

*@paramsPath 源目录

*@paramtoPath 目标目录

*@paramsourceList 源目录下文件组成的list*/

public static void recursionCopy(int sIndex,String sPath, String toPath,ListsourceList){

File toFile=newFile(toPath);

File[] toList=toFile.listFiles();//System.out.println("toList========="+toList);

try{if(toList.length==0){if(sIndex

FileUtils.copyFile(new File(sPath+sourceList.get(sIndex).getName()), new File(toPath+sourceList.get(sIndex).getName()));System.out.println("拷贝成功,文件名:"+sourceList.get(sIndex).getName()+"文件最后修改时间:"+sourceList.get(sIndex).lastModified());

Thread.sleep(50); //休眠50ms

sIndex++;

recursionCopy(sIndex,sPath, toPath,sourceList);

}else{

System.out.println("执行结束,共拷贝文件总数="+sIndex);

}

}else{//目标目录文件未处理完,再休眠50ms

Thread.sleep(50);

recursionCopy(sIndex,sPath, toPath,sourceList);

}

}catch(Exception e) {

e.printStackTrace();

System.out.println("文件拷贝异常,异常原因:"+e.getMessage());

}

}/*** 获取目录下所有文件(按时间升序排序)

*

*@parampath

*@return

*/

public static ListgetFileSort(String path) {

List list = getFiles(path, new ArrayList());if (list != null && list.size() > 0) {

Collections.sort(list,new Comparator() {public intcompare(File file, File newFile) {if (file.lastModified() >newFile.lastModified()) {return 1;

}else if (file.lastModified() ==newFile.lastModified()) {return 0;

}else{return -1;

}

}

});

}returnlist;

}/***

* 获取目录下所有文件

*

*@paramrealpath

*@paramfiles

*@return

*/

public static List getFiles(String realpath, Listfiles) {

File realFile= newFile(realpath);if(realFile.isDirectory()) {

File[] subfiles=realFile.listFiles();for(File file : subfiles) {if(file.isDirectory()) {

getFiles(file.getAbsolutePath(), files);

}else{

files.add(file);

}

}

}returnfiles;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值