public void copyFile(String srcPath, String desPath) {
try {
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(desPath);
byte datas[] = new byte[1024 * 2];
int len = 0;
while ((len = fis.read(datas)) != -1) {
fos.write(datas, 0, len);
}
fis.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Map<String, File> compoareFileMap(Map<String, File> fileMapA, Map<String, File> fileMapB) {
Map<String, File> fileMap = new HashMap<String, File>();
try {
for (String path : fileMapA.keySet()) {
if (fileMapB.get(path) == null) {
fileMap.put(path, fileMapA.get(path));
}
}
} catch (Exception e) {
e.printStackTrace();
}
int i = 0;
for (String relative: fileMap.keySet()) {
i++;
}
System.out.println("一共有"+i+"个文件不同,将检查文件最后修改时间");
return fileMap;
}
public void copyFilesByMap(Map<String, File> fileMap, String sourcePath, String desFilePath,int time) {
try {
File desFile = new File(desFilePath);
if (!desFile.exists()) {
desFile.mkdirs();
}
for (String srcfileRelativePath : fileMap.keySet()) {
File srcFile = fileMap.get(srcfileRelativePath);
File srcParentFile = srcFile.getParentFile();
String srcParentFilePath = srcParentFile.getPath();
String parentRelativePath = srcParentFilePath.substring(sourcePath.length(), srcParentFilePath.length());
File desParentFile = new File(desFilePath + "\\" + parentRelativePath);
if (!desParentFile.isDirectory()) {
desParentFile.mkdirs();
}
long srcFileTime = srcFile.lastModified();
System.out.println((System.currentTimeMillis()-srcFileTime)/60000);
if((System.currentTimeMillis()-srcFileTime)/60000<time){
copyFile(sourcePath + srcfileRelativePath, desFilePath + srcfileRelativePath);
System.out.println("复制"+sourcePath + srcfileRelativePath + "--到-->" + desFilePath + srcfileRelativePath+"完毕");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Map<String, File> getFileMap(File file, Map<String, File> fileMap, String sourceFilePath) {
try {
for (int i = 0; i < file.listFiles().length; i++) {
File childFile = file.listFiles()[i];
if (!childFile.isDirectory()) {
String chileFilePath = childFile.getPath();
String relativePath = chileFilePath.substring(sourceFilePath.length(), chileFilePath.length());
fileMap.put(relativePath, childFile);
} else {
getFileMap(childFile, fileMap, sourceFilePath);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fileMap;
}
public class Test {
public static void main(String[] args){
FileCopyDao fileCopyDao = new FileCopyDaoImpl();
try{
Map<String, File> fileMap = new HashMap<String,File>();
File file1 = new File("D:\\testspace\\thunder");
File file2 = new File("D:\\testspace\\thunder2");
Map<String, File> fileMap1 = fileCopyDao.getFileMap(file1,fileMap,file1.getPath());
System.out.println("扫描"+file1+"完毕");
fileMap = new HashMap<String,File>();
Map<String, File> fileMap2 = fileCopyDao.getFileMap(file2,fileMap,file2.getPath());
System.out.println("扫描"+file2+"完毕");
Map<String,File> map = fileCopyDao.compoareFileMap(fileMap1,fileMap2);
fileCopyDao.copyFilesByMap(map,file1.getPath(),file2.getPath(),180);
System.out.print(1);
}catch (Exception e){
e.printStackTrace();
}
}
}