Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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


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.
Posted
Comments
Richard MacCutchan 5-Sep-13 12:25pm    
Your compare routine only tests the first byte of any files, so all your results are likely to be incorrect. You also do not need to convert this to a jar file, just run the class as created by the compiler.
Gigabyte Giant 5-Sep-13 12:30pm    
Do you know why at lines 17 and 46 I am getting a InputMismatchException?

1 solution

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:
Java
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;	
    }
        
}
 
Share this answer
 
Comments
Gigabyte Giant 5-Sep-13 14:05pm    
Thank you, that worked. Now from that I should be able to change this to the way I need it.

I really appreciate it, thank you good sir.
Richard MacCutchan 6-Sep-13 3:45am    
Happy to help, good luck with your further efforts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900