JavaXT
|
|
Directory SearchThis javaxt.io.Directory class provides several methods to search for files and directories:
Searching for FilesThere are several different ways to search for files in a directory. Here are a few examples:javaxt.io.Directory directory = new javaxt.io.Directory("/temp"); javaxt.io.File[] files; //Return a list of all files found in the current directory files = directory.getFiles(); //Return a list of all files found in the current directory and in any subdirectories files = directory.getFiles(true); //Return a list of PDF documents found in the current directory and in any subdirectories files = directory.getFiles("*.pdf", true); File FiltersIn the last example, we performed a basic basic wildcard search for PDF documents (files that end with a .pdf extension). We did this my specifying a "*.pdf" file filter when we called getFiles(). You can also can pass in multiple wildcards using an String array. Here's an example of a resursive directory search using multiple wildcards.String[] filter = new String[]{"*.txt", "*.htm"}; javaxt.io.File[] files = directory.getFiles(filter, true); Of course, you can also use a standard java.io.FileFilter. Here's a simple example used to find txt files. java.io.FileFilter filter = new java.io.FileFilter() { public boolean accept(java.io.File file) { if (file.isHidden()) return false; else{ if (file.isDirectory()) return true; else return (file.getName().endsWith(".txt")); } } }; javaxt.io.File[] files = directory.getFiles(filter, true); Efficient SearchIn the previous examples, we used the getFiles() method to search for files. The getFiles() method returns an array of files. This is OK for small directories, but what if you want to search for tens of thousands of files? Waiting for the getFiles() method to return might take several minutes or even hours to complete and would require large amounts of memory. Instead, you can use the getChildren() method and process files and directories as soon as they are found. Here's an example used to return all files and directories in a directory using the getChildren() method. In this example, we will perform a recursive directory search and process files and directories as soon as they are found. //Set flags boolean recursiveSearch = true; Object fileFilter = null; boolean wait = false; //Initiate search java.util.List results = directory.getChildren(recursiveSearch, fileFilter, wait); while (true){ Object item; synchronized (results) { //Wait for files/directories to be added to the list while (results.isEmpty()) { try { results.wait(); } catch (InterruptedException e) { break; } } //Grab the next available file/directory from the list item = results.remove(0); results.notifyAll(); } //Do something with the file/directory if (item!=null){ if (item instanceof javaxt.io.File){ javaxt.io.File file = (javaxt.io.File) item; System.out.println(file); } else{ javaxt.io.Directory dir = (javaxt.io.Directory) item; System.out.println(dir); } } else{ //Item is null. This is our queue that the search is done! break; } } Recursive SearchRecursive directory searches highlighted in the examples above are extremely fast. This is thanks, in large part, to the multi-threaded design of the javaxt.io.Directory class. Additional efficiencies are gained on Windows file systems by leveraging native Windows API (via the javaxt-core JNI). On Windows file systems, the directory search is approximately 2x faster than the java.io.File listFiles() method on local file systems and over 10x faster searching network drives.Additional Options for WindowsThe javaxt.io.Directory class offers several additional capabilities specific for JVMs running on Windows. Among these is the ability to list all the shared drives found on a server. Example:String serverName = "\\\\Orion"; //<--Can also be an IP address like "\\192.168.0.1" for (javaxt.io.Directory share : new javaxt.io.Directory(serverName).getSubDirectories()){ System.out.println(share); }Another Windows-specific feature is the ability to list all the root/logical drives on the host machine, including disconnected network drives. This is something not available in the standard Java release (java.io.File.listRoots() does not return disconnected network drives on Windows). for (javaxt.io.Directory drive : javaxt.io.Directory.getRootDirectories()){ System.out.println(drive); } |