2016-05-13 6 views
0

私はここに内容の後の2つのファイルを比較するプログラムを持っていますが、私はそれが大丈夫かもしれないと思っていました。しかし、それが他のファイルタイプのためにどのように、それはハッシュ値を比較するのが好きかもしれませんか?ハッシュ値とコンテンツの確認を確認しますか?

誤解して申し訳ありません

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

答えて

0

あなたの上記の溶液は、テキストファイルを比較するために十分であるかもしれません。任意のファイルを比較したい場合は、guava apiを試してください。すでに議論されており、ここで回答しています:Java: How to check that 2 binary files are same?

関連する問題