hi, im trying to compare the md5 key of a file selected with the md5 key of known files stored in a text file. i can get the md5 key of the file selected and all & also read the text file line by line however, i cant seem to compare it properly. can someone check my codes and see if what im doing is right? or is there any way to compare that key with each line of the stored file.thanks :)

public void onAdd(Object newResource) {
    	
        if (newResource instanceof File) {
            File file = (File) newResource;
            if (file.isFile()) 
            {
                System.out.println(file.getAbsolutePath() + " is added");
                try{
                    	 	MessageDigest msgD=MessageDigest.getInstance("MD5");
                    		FileInputStream fileStr = new FileInputStream((file.getAbsolutePath()));
                    		
                    		byte[]dataBytes=new byte[1024];
							int nread=0;
							while((nread=fileStr.read(dataBytes))!=-1){
							msgD.update(dataBytes,0,nread);
							};
							byte[]mdbytes=msgD.digest();
							
							StringBuffer t = new StringBuffer();
							for(int i=0;i<mdbytes.length;i++){
							t.append(Integer.toString((mdbytes[i] & 0xff)+0x100,16).substring(1));
							}
							System.out.println("MD5 key stored : "+t.toString());
							
							//scan both md5
							try
							{
								FileReader input=new FileReader("test.txt");
								BufferedReader bufRead=new BufferedReader(input);
			
								String line;
								int count=0;
								line=bufRead.readLine();
								count++;
								
								if(bufRead.readLine().equals(t.toString()))
								{
									System.out.println("DENIED");
								}
								else
								{
									System.out.println("ALLOWED");
								}
								
							while(line!=null)
							{
								System.out.println(count+":"+line);
								line=bufRead.readLine();
								count++;

							}
								
								bufRead.close();
								
								
							}catch(ArrayIndexOutOfBoundsException e)
							{
								e.printStackTrace();
							}catch(IOException e)
							{
								e.printStackTrace();
							}
							//ends		
							
							
										
                }catch(Exception e)
                {
                	
                }
            }
        }
      
        
    }

Please use the code tags when posting code

bufRead.readLine().equals(t.toString()

Try debugging your code by Displaying the value of every line you read and the value of the String t.

urmmm..
i do display it if u check my code..
rite?

Can you copy and print the output from the program and explain why "i cant seem to compare it properly."
Where is the compare that isn't proper?

okay. manage to solve it. it was due to the line "line=bufRead.readLine()".
anyway, now im faced with another problem.
from this line of codes, i would like to compare 2 different md5 key which is, 1 scanned from the file selected & d other, from a text file.
i manage to do it & would like to have the selected file deleted if the md5 key matches any of the keys in the text file. however, i fail to do so cos although manage to compile,it does not show the file being deleted. not only that, after I did a few "delete" methods, i realised that the comparing does not work already. anyone can help? thanks

import java.io.*;
import java.util.*;
import java.security.MessageDigest;

public class FileListener extends BaseListener implements IFileListener {


    /**
     * Connstructor
     */
    public FileListener() {
        super();
    }

    public void onStart(Object monitoredResource) {
        // On startup
        if (monitoredResource instanceof File) {
            File resource = (File) monitoredResource;
            if (resource.isDirectory()) {

                System.out.println("Start to monitor " + resource.getAbsolutePath());

                /*File[] files = resource.listFiles();
                for (int i = 0; i < files.length; i++) {
                    File f = (File) files[i];
                    onAdd(f);
                }*/
            }
        }
    }

    public void onStop(Object notMonitoredResource) {

    }

    public void onAdd(Object newResource) {
    	
        if (newResource instanceof File) {
            File file = (File) newResource;
            if (file.isFile()) 
            {
            	
                System.out.println(file.getAbsolutePath() + " is added");
                try{
                    	 	MessageDigest msgD=MessageDigest.getInstance("MD5");
                    		FileInputStream fileStr = new FileInputStream((file.getAbsolutePath()));
                    		
                    		byte[]dataBytes=new byte[1024];
							int nread=0;
							while((nread=fileStr.read(dataBytes))!=-1){
							msgD.update(dataBytes,0,nread);
							};
							byte[]mdbytes=msgD.digest();
							
							StringBuffer t = new StringBuffer();
							for(int i=0;i<mdbytes.length;i++){
							t.append(Integer.toString((mdbytes[i] & 0xff)+0x100,16).substring(1));
							}
							System.out.println("MD5 key stored : "+t.toString());
							
							//scan both md5
							try
							{
								FileReader input=new FileReader("md5.txt");
								BufferedReader bufRead=new BufferedReader(input);
			
								String line;
								int count=0;

								line = bufRead.readLine();
								count++;
						
						
							 
								if(line.equals(t.toString()))
								{
									System.out.println("DENIED");
									
								}
								else
								{
									System.out.println("ALLOWED");
								}
								
								while(line!=null)
								{
									System.out.println(count+":"+line);
									line=bufRead.readLine();
									count++;								
								}

								bufRead.close();
							
								
							}catch(ArrayIndexOutOfBoundsException e)
							{
								e.printStackTrace();
							}catch(IOException e)
							{
								e.printStackTrace();
							}
							//ends		
							
							
										
                }catch(Exception e)
                {
                	
                }
            }
        }
      
        
    }
    public void onChange(Object changedResource) {
        if (changedResource instanceof File) {
            File file = (File) changedResource;
            if (file.isFile()) {
                System.out.println(file.getAbsolutePath() + " is changed");
            }

        }
    }

    public void onDelete(Object deletedResource) {
        if (deletedResource instanceof String) {
            String deletedFile = (String) deletedResource;
            System.out.println(deletedFile + " is deleted");
        }
    }
    
    
}

the comparing does not work

Can you comment your code to show where the comparing is?

sorry...
anyway here it is...
from line 76 to 86

if(line.equals(t.toString()))
								{
									System.out.println("DENIED");
 
								}
								else
								{
									System.out.println("ALLOWED");
								}

sorry...
anyway here it is...
from line 76 to 86

if(line.equals(t.toString()))
								{
									System.out.println("DENIED");
 
								}
								else
								{
									System.out.println("ALLOWED");
								}

Do some debugging by adding println() statements to show the values of
line and t.toString() so you can see them.

ok just did!
i addded

System.out.println(""+line);
							 	System.out.println(""+t.toString());

just before the line 76
and it works at times n it doenst work at times.
this is cos it scans only the 1st line.
but why so? yesterday it worked.
any idea?

it doesn't work at times

Sounds like its time to do some more debugging.

yesterday it worked.

What have you changed? Computers are pretty consistent.

nth changed. but nvm, ill do more debugging.
anyway, from that source code, what would be the best way to delete the file based on the matching md5 key?
i did

File file2=new File(location);
file2.delete();

n i did specify that

location=file.getAbsolutePath();

but although the key matches, it does not delete the file.
am i doing the right thing?

nth changed. but nvm, ill do more debugging.
anyway, from that source code, what would be the best way to delete the file based on the matching md5 key?
i did

File file2=new File(location);
file2.delete();

n i did specify that

location=file.getAbsolutePath();

but although the key matches, it does not delete the file.
am i doing the right thing?

Have you tried the exists() method?
Have you looked the return from delete()?

care to explain more? thanks

Test if the file exists with the exists() method. Maybe your path is wrong.
Display the value returned from the delete() method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.