Your code works fine after removing the first line. Then it prints a list of files, with full path.
If you also want to print MD5 sums, you can just pick the relevant parts from the code you refer. Which are not too many, as you have File objects in the loop already, so you need that single line with the md5 thing:
public static void listFilesAndFilesSubDirectories(String directoryName){
File directory = new File(directoryName);
//get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList){
if (file.isFile()){
System.out.print(file.getAbsolutePath());
try(FileInputStream fis=new FileInputStream(file)){
System.out.println(" - MD5: "+DigestUtils.md5Hex(IOUtils.toByteArray(fileInputStream)));
}catch(Exception ex){
System.out.println(" - Error: "+ex);
}
} else if (file.isDirectory()){
listFilesAndFilesSubDirectories(file.getAbsolutePath());
}
}
}
As I do not really like the dependence on an external library, and especially the fact that it loads the entire file in the memory (the toByteArray call indicates it), here is a replacement for the first if, without Apache Commons and without loading the entire file into an array, but requiring a throws NoSuchAlgorithmException for the method header or an extra try-catch somewhere:
...
if (file.isFile()){
System.out.print(file.getAbsolutePath());
try(DigestInputStream dis=new DigestInputStream(new BufferedInputStream(new FileInputStream(file)), MessageDigest.getInstance("MD5"))){
while(dis.read()>=0);
System.out.println(" - MD5: "+javax.xml.bind.DatatypeConverter.printHexBinary(dis.getMessageDigest().digest()));
}catch(Exception ex){
System.out.println(" - Error: "+ex);
}
} else if (file.isDirectory()){
...
Or a long one which throws no exception and does not depend on javax stuff either (which is not necessarily present after all):
...
if (file.isFile()){
System.out.print(file.getAbsolutePath());
MessageDigest md5=null;
try{md5=MessageDigest.getInstance("MD5");}catch(NoSuchAlgorithmException nsae){};
try(DigestInputStream dis=new DigestInputStream(new BufferedInputStream(new FileInputStream(file)), md5)){
while(dis.read()>=0);
System.out.print(" - MD5: ");
for(Byte b: md5.digest())
System.out.printf("%02X",b);
System.out.println();
}catch(IOException ioe){
System.out.println(" - Error: "+ioe);
}
} else if (file.isDirectory()){
...
Here is the actual code (RecDir.java) I used for testing, now modified for c:\ (which includes an additional check for dealing with directories you have no right to access):
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class RecDir {
public static void main(String[] args) {
listFilesAndFilesSubDirectories("c:\\");
}
public static void listFilesAndFilesSubDirectories(String directoryName){
File directory = new File(directoryName);
//get all the files from a directory
File[] fList = directory.listFiles();
if(fList!=null)
for (File file : fList){
if (file.isFile()){
System.out.print(file.getAbsolutePath());
MessageDigest md5=null;
try{md5=MessageDigest.getInstance("MD5");}catch(NoSuchAlgorithmException nsae){};
try(DigestInputStream dis=new DigestInputStream(new BufferedInputStream(new FileInputStream(file)), md5)){
while(dis.read()>=0);
System.out.print(" - MD5: ");
for(Byte b: md5.digest())
System.out.printf("%02x",b);
System.out.println();
}catch(IOException ioe){
System.out.println(" - Error: "+ioe);
}
} else if (file.isDirectory()){
listFilesAndFilesSubDirectories(file.getAbsolutePath());
}
}
}
}
I just ran it directly from NetBeans/Eclipse project folder (that is what the hardcoded "." results in), and then it lists various project files in the subdirectories, the .java file itself, etc.