import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class FileSyncApp
{
public static void main(String[] args) throws Exception
{
String sourcePath = "E:/03_Movie";
String destinationPath = "H:/03_Movie";
List
List
for (File sourceFile : sourceFileList)
{
String filePath = sourceFile.getAbsolutePath();
filePath = filePath.replace("\\", "/");
filePath = filePath.replace(sourcePath, "");
// System.out.println("sourceFilePath=" + filePath);
sourceFilePathList.add(filePath);
}
/*
* Delete an unexist file in destination path
*/
List
for (File destinationFile : destinationFileList)
{
String filePath = destinationFile.getAbsolutePath();
filePath = filePath.replace("\\", "/");
filePath = filePath.replace(destinationPath, "");
/*
* Delete non-exists file
*/
if (!sourceFilePathList.contains(filePath))
{
if (destinationFile.delete())
{
System.out.println("deleted=" + filePath);
}
}
}
System.out.println("Start comparing file");
for (String sourceFilePath : sourceFilePathList)
{
File sourceFile = new File(sourcePath + sourceFilePath);
// System.out.println(sourceFile.getAbsolutePath());
File destinationFile = new File(destinationPath + sourceFilePath);
if (destinationFile.exists())
{
long sourceFileSize = FileUtils.sizeOf(sourceFile);
long destinationFileSize = FileUtils.sizeOf(destinationFile);
if (FileUtils.isFileNewer(sourceFile, destinationFile) || FileUtils.isFileOlder(sourceFile, destinationFile))
{
destinationFile.delete();
System.out.println("Modified-Date diff, Delete File=" + destinationFile.getAbsolutePath());
} else if (sourceFileSize != destinationFileSize)
{
destinationFile.delete();
System.out.println("Size diff, Delete File=" + destinationFile.getAbsolutePath());
} else
{
continue;
}
}
System.out.println("I:Copy File=" + destinationFile.getAbsolutePath());
FileUtils.copyFile(sourceFile, destinationFile, true);
System.out.println("O:Copy File=" + destinationFile.getAbsolutePath());
}
System.out.println("Done");
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น