-3

I have a text file that stores hash values of files generated in a Java program and I would like to write Java codes to compare the hash values stored in the file to see if they match or not.

For example, the text file (md5.txt) contains the following:

File: file.doc
Hash: 0dcf2e7a00cf1b9673ddc7b699e93aa9
File: file-copy.doc
Hash: 0dcf2e7a00cf1b9673ddc7b699e93aa9

The hash values are on lines 2 and 4. Therefore, is it possible to compare alternate lines such as 2 and 4, 6 and 8 and so on?

user3847620
  • 59
  • 4
  • 9

2 Answers2

0

You can store the lines which begins with Hash: in an HashSet to compare it to all previous hash codes (you simply need to look at what returns the add method of the HashSet).

Dici
  • 25,226
  • 7
  • 41
  • 82
0

You can start you code like this.

BufferedReader br=new BufferedReader(new FileReader(new File("/home
                                                          /ruchira/Test.txt")));
List<String> list=new ArrayList<>();
while (br.readLine()!=null){  // reads 1st,3rd,5th,... lines here
    list.add(br.readLine());  // reads 2nd, 4th, 6th,..lines here
}
// now you have a list of hash value check whether same file is here.
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • @RuchiraGayanRanaweera this would throw exception if there are odd number of lines. so would it not better to check br.readline() within the loop ? – nafas Aug 08 '14 at 12:05