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