一个java文件夹比较的小程序

本文介绍了一个用于比较两个文件夹中文件差异的Java工具。该工具能够列出源文件夹中不存在于目标文件夹的文件及路径,并反之亦然。通过递归遍历文件夹,此工具可以有效地找出文件差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值