package com.zhey.compare;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
public class Compare {
private String sourceFold = null;
private String destinationFold = null;
private List<String> dstNotExist = new ArrayList<String>();
private List<String> srcNotExist = new ArrayList<String>();
public String getSourceFold() {
return sourceFold;
}
public void setSourceFold(String sourceFold) {
this.sourceFold = sourceFold;
}
public String getDestinationFold() {
return destinationFold;
}
public void setDestinationFold(String destinationFold) {
this.destinationFold = destinationFold;
}
public List<String> getDstNotExist() {
return dstNotExist;
}
public void setDstNotExist(List<String> dstNotExist) {
this.dstNotExist = dstNotExist;
}
public List<String> getSrcNotExit() {
return srcNotExist;
}
public void setSrcNotExit(List<String> oldNotExit) {
this.srcNotExist = oldNotExit;
}
public void CompareThey() throws FileNotFoundException {
File source = null;
File destination = null;
if (sourceFold == null) {
throw new FileNotFoundException("Source fold is not found");
}
if (destinationFold == null) {
throw new FileNotFoundException("destination fold is not found");
}
source = new File(sourceFold);
if (!source.exists()) {
throw new FileNotFoundException("Source fold is not found");
}
destination = new File(destinationFold);
if (!destination.exists()) {
throw new FileNotFoundException("destination fold is not found");
}
compareFold(sourceFold, destinationFold);
}
private void compareFold(String srcPath, String dstPath) {
File src = new File(srcPath);
File dst = new File(dstPath);
boolean exist = false;
if (src.isFile()) {
return;
}
File[] srcFiles = src.listFiles();
File[] dstFiles = dst.listFiles();
List<String> exitFiles = new ArrayList<String>();
for (File file : srcFiles) {
for (File file1 : dstFiles) {
if (file.getName().equals(file1.getName())) {
if ((file.isFile() && file1.isFile()) || (file.isDirectory() && file1.isDirectory())) {
if (file.isDirectory()) {
exitFiles.add(file.getName());
}
exist = true;
break;
}
}
}
if (!exist) {
dstNotExist.add(file.getAbsolutePath());
} else {
exist = false;
}
}
for (File file : dstFiles) {
if (!exitFiles.contains(file.getName())) {
srcNotExist.add(file.getAbsolutePath());
}
}
for (String file : exitFiles) {
compareFold(srcPath + File.separator + file, dstPath + File.separator + file);
}
}
}
一个java文件夹比较的小程序
最新推荐文章于 2021-09-08 21:20:58 发布