1

I want to design a file-system crawler for Linux and Windows OS in java. I am new to java and I am building an application that searches for matching files for a given file-name in the file-system. For Linux I have come up with the following strategy and my algorithm is as follows:-

  1. Open the root directory ("/") using the file open methods in java.
  2. Search in the current directory for the filename and record the path if search is successful for the file.
  3. Record the name of all directories in the current directory
  4. Visit each directory recursively and record the full path name if search is successful.
  5. After accessing the entire filesystem, list the results.

How do I search for a file, in a similar fashion, in the Windows file-system (NTFS)?. How do I find the root?

I know this may be a VERY inefficient method to scan for files or I may be going off track but please do suggest methods or post links where I can find useful information to accomplish my project. One such link that I found was the following one: https://docs.oracle.com/javase/tutorial/essential/io/walk.html

I wanted to know if implementing the FileVisitorInterface is correct and efficient or some other ways exist to do what I intend to. I am a newbie in java hence I don't know where to start and where to look. Many thanks in advance for all your answers!

Timeout
  • 7,759
  • 26
  • 38
physio
  • 109
  • 1
  • 13

1 Answers1

-1

I have built a similar tool in the past. What you have described sounds like a good approach for traversal. A filesystem is after all just a tree. Nothing wrong with traversing a tree recursively. Can't do better than traversing the whole thing in O(n) if you are doing a find.

For windows you can get a list of all the drives with something like:

File[] roots = File.listRoots();

You would get an array with something like [C:\ , D:\ , E:\ , F:\ ];

With the traversal you'll have to decide if you want to do breadth first of depth first, but since you don't stop traversing after finding the first file it shouldn't matter which method you choose.

boris
  • 57
  • 4
  • this is not even 1/4 of an answer. –  Feb 22 '16 at 03:42
  • 1
    He asked how his approach differs under windows as opposed to linux. I confirmed it would be the same. He asked how to get the root directory under windows, I provided a code snippet. He was concerned his recursive traversal was inefficient, I commented that you can't recursively search an entire tree faster than O(n). What else should I address? – boris Feb 22 '16 at 03:47