1

In the recent versions of windows, you can hit the start menu and start typing to search for files across the filesystem. Is there a way to do that programmatically in Java?

My specific purpose is to allow the user to choose a file or directory. The user could start typing a file name directly inside the application and it could start showing suggestions. Much easier than navigating directories in a typical file chooser.

mentics
  • 6,852
  • 5
  • 39
  • 93
  • Doesn't every operating system have this kind of feature, though? Windows 7 has an awesome Start Menu with improved search, Windows XP has a search function that only requires one more click, Mac OS has Spotlight which only requires one more keypress, and most types of Linux have a search function similar to XP's... – Ry- Aug 16 '11 at 20:57

4 Answers4

1

What you're asking for in the second part of your question could be done with a simple:

File dir = new File( "/path/to/dir" );
String[] contents = dir.listFiles();

The first part is a bit harder, but it could be accomplished inside Java using the same technique called recursively. (for(String fl: contents){ /* do the above */ })

You don't want to worry about supporting wrappers around command line calls here. That is anti-platform independent.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • This is a linear search. Modern systems may have many files. This may be very slow and consume significant resources. The operating system's file query service can search more quickly. – Andy Thomas Aug 16 '11 at 21:11
  • Realistically, he's not going to be looking through more than one or two folders at once. From the OP's q, the OP may only be looking at one at a time. Does he really want to support `find /` vs. `dir C:\ /s` – cwallenpoole Aug 16 '11 at 21:22
  • From the question: "search for files across the filesystem ... Much easier than navigating directories in a typical file chooser." – Andy Thomas Aug 16 '11 at 21:28
  • @Andy But he also says that he is still dealing with a seed directory meaning it does not necessarily need to have all of the data. I'll concede that it is a case-by-case basis, but I think that this is the bettter bet most of the time. – cwallenpoole Aug 16 '11 at 21:31
  • I don't see any reference to a "seed directory." I think you're answering a different question than asked here. – Andy Thomas Aug 16 '11 at 21:43
  • @Andy Hm... misread `My specific purpose is to allow the user to choose a file or directory` – cwallenpoole Aug 16 '11 at 21:43
  • @cwallenpoole is correct, I want to allow the user to search the entire filesystem. In many cases, they will remember something of the filename they are looking for and so we can avoid having to browse all over because I often forget where I saved a certain file. – mentics Aug 17 '11 at 19:07
1

you could write JNI code to wrap+expose any windows native functionality.

If it's just for autocomplete style behavior in a file dialog, you don't need to wrap the native functionality, you can just use standard stuff in java.io.

John Gardner
  • 24,225
  • 5
  • 58
  • 76
  • Or you might find JNA easier. Personally, I used JNA to a native, platform-neutral glue API, implemented so far with Mac Spotlight. – Andy Thomas Aug 16 '11 at 21:22
1

This post details a way to do this in Windows, by accessing the native functionality:

Query Windows Search from Java

And this one details how you can do something similar (using mdfind) under Mac OS:

http://lists.apple.com/archives/Java-dev/2006/Oct/msg00224.html

Community
  • 1
  • 1
0

Sounds unlikely ti be a part of Java, since Java is supposed to be platform-independent. Such searches should require some sort of index over all files, to be efficient, also.

Per Alexandersson
  • 2,443
  • 1
  • 22
  • 28
  • It would fit well within the Java 7 NIO.2 file system API, which has platform-dependent features. The index already exists on some operating systems. – Andy Thomas Aug 16 '11 at 21:15