Java重复文件查找器 [英] Java Duplicate File Finder

查看:52
本文介绍了Java重复文件查找器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



所以我找到了一个人的工作来制作一个应用程序,列出当前工作目录中的重复文件。



最终它会让你选择你可以用重复文件做什么,但是,现在它只是列出它们。



我正在使用Java(根据选择),我为此写了大约50行代码。它花了我大约20分钟,我在eclipse中运行了一个测试,我得到了InputMismatchException。但是,当应用程序运行时,我做对了。所以我继续编译项目以在命令提示符中测试它,当我运行它时(使用java -jar Duplicates.jar)它说:



No Main Manifest属性,在Duplicates.jar中



现在我觉得编译时出错了,我只是想搞清楚我为什么或如何获得InputMismatchException。它说它发生在第17和第46行。下面是我的代码,我对第17和第46行有评论。



Hello everyone,

So I was presented a job by someone to make an application that would list duplicate files in the current working directory.

Eventually it will give you a choice on what you can do with the duplicate files, but, for now it is going to just list them.

I am using Java (by choice), and I wrote out about 50 lines of code for this. It took me about 20 minutes, and I ran a test inside of eclipse and I got the InputMismatchException. However I get it right when the application goes to run. So I went ahead and compiled the project to test it in Command Prompt, and right when I run it (using java -jar Duplicates.jar) it says:

No Main Manifest Attribute, in Duplicates.jar

Now I think that was something that went wrong when I compiled, I just want to figure out why or how I am getting an InputMismatchException. It says it is occurring at lines 17 and 46. Below is my code, I have comments on lines 17 and 46.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class DuplicateFileRemover {
	public static boolean CompareFiles(File x, File y) { 
		try {
			Scanner xs = new Scanner(x);
			Scanner ys = new Scanner(y);
			boolean result = true;
			while (result == true) {
				if (xs.nextByte() != ys.nextByte()) { // Line 17
					result = false;	
				}
				return result;	
			}
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());	
		}
		return false;	
	}
	public static void main(String[] args) {
		File dir = new File(".");
		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])) { // Line 46
					System.out.println(fileList[x]);
				}	
			}
		}
		
	}
	
}





任何人都可以看到我的代码有问题吗?由于我正在处理文件,我知道我可能需要捕获可能的FileNotFound异常。



再次,就像我之前说的那样,不需要用户输入,应用程序仅在获取当前工作目录中的文件时运行。





请帮助。



Can anyone see a problem with my code? Since I am dealing with files I knew I probably needed to catch a possible FileNotFound exception.

Again, like I said before, there is no user input required, the application runs solely on getting files in the current working directory.


Please help.

推荐答案

你的问题是由于 Scanner 类试图从输入文件解释标记并将它们转换为字节。你需要一个基本的流阅读器,它允许你从文件中读取单个字节。



试试这个:

Your problem is due to the Scanner class trying to interpret the tokens from the input file and convert them to bytes. You need a basic stream reader that will allow you to read individual bytes from the file.

Try this:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileInputStream;


public class Test {
 
    /**
    * @param args
    */
    public static void main(String[] args) {
        File dir = new File(".");
        File[] fileList = dir.listFiles();
        for (int x = 0; x < fileList.length; x++) {
            for (int y = x+1; y < fileList.length; y++) {
                if (fileList[x].length() == fileList[y].length()) {
                    if (CompareFiles(fileList[x], fileList[y])) {
                        System.out.println(fileList[y]);
                    }
                }
            }
        }
    }

    public static boolean CompareFiles(File x, File y) {
        try {
            FileInputStream xs = new FileInputStream(x);
            FileInputStream ys = new FileInputStream(y);
            System.out.println("Compare: " + x + " vs " + y);
            boolean result = true;
            while (result == true) {
                int xb = xs.read();
                int yb = ys.read();
                if (xs.read() != ys.read()) {
                    result = false;
                    break;
                }
                if (xb == -1)
                    break;
            }
            return result;	
            } catch (FileNotFoundException e) {
                return false;
            } catch (Exception e) {
                System.out.println(e.getMessage());	
            }
        return false;	
    }
        
}


这篇关于Java重复文件查找器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆