我想计算发生字符串的no文件,并且我在目录中有一个文档列表,但它们是多余的。如何从该特定目录中删除重复文件? 任何帮助表示赞赏!
public static boolean CompareFiles(File x, File y) throws FileNotFoundException
{ //boolean result=true;
try {
Scanner xs = new Scanner(x);
Scanner ys = new Scanner(y);
boolean result = true;
while (result)
{
if (xs.nextByte() != ys.nextByte()) result = false;
}
return result;
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
return false;
}
}
public static void main(String[] args) throws FileNotFoundException, IOException//
{
File dir = new File("C:/Users/Aravind/Documents/ranked");
File[] fileList = dir.listFiles();
for (int x = 0; x <fileList.length; x++)
{
for (int y = x+1; y < fileList.length; y++)
{
if (CompareFiles(fileList[x],fileList[y]))
{
System.out.println("in calling fn");
fileList[x].delete();
}
//System.out.println(fileList[x]);
}
}
答案 0 :(得分:2)
使用文件名作为键创建映射,并使用文件的校验和作为值(按照此example使用java获取文件的校验和)。
在向该地图添加新条目之前,请检查计算的校验和是否已经存在containsValue
(如果两个文件具有相同的校验和,其内容相同)。
删除“冗余”文件。
答案 1 :(得分:0)
for (File f : dir.listFiles()) if (isDuplicate(f)) f.delete();
...或者可能会向我们提供您需要的更多详细信息。