我想统计没有发生字符串的文件,并且我有一个目录中的文档列表,但它们是多余的。如何从该特定目录中删除重复文件? 任何帮助表示赞赏!如何删除特定目录中的重复文件
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]);
}
}
您可以在单个目录中创建冗余文件吗? – 2012-04-20 12:23:50
你的意思是像'〜somedoc.txt'这样的文件,它似乎是'somedoc.txt'等的重复文件吗?如果是这样,只要检查文件名是否以'〜'开头。否则,请更具体一些,也许提供一个例子。 – Thomas 2012-04-20 12:33:36
我正在索引中搜索一个单词,并基于该目录在目录中创建文件,因此存在多余的文件。 Ya Imean文件的内容是多余的,而不是文件的名称。 – 2012-04-20 12:35:15