2016-05-13 148 views
0

我在這裏有一個程序比較內容後的兩個文件,我認爲比較它的文本可能沒問題。但它是如何用於其他文件類型的,它可以用來比較散列值嗎?檢查哈希值與檢查內容?

對不起任何誤解

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 !!!"); 
     } 
    } 
} 
+0

如果您必須計算散列值,則比較散列值會嚴格較慢。 –

+0

你是什麼意思'比較哈希值'。在你的代碼中沒有這樣的東西。 – pvg

+0

另請參閱http://stackoverflow.com/questions/27379059/determine-if-two-files-store-the-same-content作爲一個可能的騙局 – pvg

回答