检查哈希值与检查内容?

时间:2016-05-13 20:55:34

标签: java file hash types

我这里有一个比较内容之后的两个文件的程序,我想文本比较它可能没关系。但是对于其他文件类型如何,比较哈希值是否有用呢?

对于任何误解感到抱歉

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

public class FileToFileC {
    private File cf;
    private File cf2;
    private JFileChooser chooser = new JFileChooser();
    private File direc = null;
    private String s1 ="";
    private String s2 ="";


    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        new FileToFileC().los();
    }

    private void los() throws IOException, InterruptedException, Exception {
        System.out.println("FileComparing");
        System.out.println("--------------------------------------------------------------");
        System.out.println("**************************************************************");
        System.out.println("--------------------------------------------------------------");
        System.out.println("1 - Compare Files /// 0 - Exit");
        System.out.println("                                                              ");
        System.out.println("Choose:");
        Scanner sc = new Scanner(System.in);
        String temp = sc.next();
        //sc.close();

        switch (temp) {
            case "1":
                openFchooser1();
                break;

            case "0":
                break;
            default:
                System.out.println("\nplease, type only 1 or 0 !");
        }
    }


    private void openFchooser1() throws FileNotFoundException, InterruptedException, IOException, Exception {
        int returnVal = chooser.showDialog(null, "Choose the first File");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            direc = chooser.getSelectedFile();
            readFromFile(direc);
            openFchooser2();
        }
    }

    private void openFchooser2() throws FileNotFoundException, InterruptedException, IOException, Exception {
        int returnVal = chooser.showDialog(null, "Choose the second File");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            direc = chooser.getSelectedFile();
            readFromFile2(direc);
            fileC();
        }
    }

    private void readFromFile(File cf2) throws IOException, Exception {
        FileReader fr = new FileReader(cf2);
        try (BufferedReader bw = new BufferedReader(fr)) {
            while(bw.readLine() != null) {
                s1 += bw.readLine();
            }
            System.out.println("Wait while reading !");
        }
    }

    private void readFromFile2(File cf2) throws IOException, Exception {
        FileReader fr = new FileReader(cf2);
        try (BufferedReader bw = new BufferedReader(fr)) {
            while(bw.readLine() != null) {
                s2 += bw.readLine();
            }
        }
    }

    private void fileC() {
        System.out.println("Wait while comparing !");
        if (s1.equals(s2)) {
            System.out.println("Files are equal !!!");
        } else {
            System.out.println("Files are wrong !!!");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的上述解决方案可能足以比较文本文件。如果你想比较任何文件,请尝试guava api。这已经在这里讨论和回答:Java: How to check that 2 binary files are same?