Paid for writing

In case you like my writing and would like me to write for your website, then please leave a comment to any of my blog article, mentioning your Email Id and I will reply back. Thanks

Friday, January 30, 2009

Explanation for the source code in Java to list all files in your System

This is an explanation of the FileCrawl.java

The code written in Java searches through all the devices present on your system and creates a list of all the file names present inside them.
The devices which this code searches include the hard drives, the disk drives and any usb device connected.
The code is written in Java 1.6 and uses the java.io.File class to parse through the directories
It stores the output in a file "D:\\output.txt". This is a hardcoded file name inside the code.

The code can be explained as below

1. The code uses the java.io.File class to parse through the directories within your devices.
2. The main creates an object of this class FileCrawl.
3. The constructor does two things :
a) Create a new object of the class FileWriter, which is used to create a file wherein we store the list of file names.
b) Create an object of the class listRoots
4. The object fw of the FileWriter class mentions the name of the file as the first paramater and mentions true in the second parameter which means the file should be opened in append mode.
5. The listRoots function in the File class displays the list of the available file system roots. I tested this code on Windows so for me the list of available file system roots was my drives (c:\,d:\,e:\);
6. Back in the main function, the the fileRoots variable which stores the value from Files.listRoots() is parsed and for each value present in that File array the read function is called.
7. The two ArrayList files and dir are static meaning that they will maintain theur values in between function calls. Like for instance if these two ArrayLists were not static then by the time I returned from the read function and went back to it, the contents of this ArrayList will be lost. So in order to store the values between function calls this ArrayList is declared as static.
8. The dir ArrayList stores the list of all the directories in your system.
9. The files ArrayList stores the list of all the files in your system.
10. The fileList creates a new instance of an class File.
11. The drive name is added to the drive ArrayList and the for loop begins which finds all the directories and files in this drive.
12. The fileList, which is an instance of class File takes the value from the dir ArrayList
13. It listFiles() function returns all the files and directories, present immediately within the path the fileList is referencing to. This means that it does not searches inside the directories present in this current directory
14. The if condition for null is true when the tempFile (i.e. the directory) does not contain any files within it.
15. In case its not null, then for every element present in tempFile, a second if condition checks whether its a directory.
16. If its a directory then its added to a dir ArrayList, else its considered as a file and added to the files ArrayList.
17. In case the tempFile is null, then we just continue and go back to the next iteration of the for loop.
18. So basically what happens at the end of one single iteration of the for loop is that if fileList currently points to the directory C:\windows, then this single iteration of the for loop will return me all the files and directories within C:\windows\ only.
19. As more and more directories get added to the dir ArrayList, the size increases.
20. The for loop runs for the entire size of the ArrayList, meaning for all directories present in that drive.
21. At the end of this read function it just prints the total size of the ArrayList files, i.e. the total no. of files found in that particular drive.
22. It is this list which is written to the file, which fw object is pointing to.
23. At the end of this read function , the control goes back to main() which sends the next drive name(if any) as input to read() and the process continues, till all the drives are completed.
24. This code obviously will take time considering the sheer size of the drives it has to scan, but it works nevertheless

0 comments: