Re: Search for a file on hard drive?
Eric Noriega <[email protected]>
| Newsgroups | gmane.comp.java.sun.servlet |
|---|---|
| Message-ID | <[email protected]> |
These are all correct, but not very practical examples. You don't
want to go using stack based recursion in any production application. I
thought I would post a bit of code I had whipped up. Keep in mind that
this is not a fully developed application. This requires j2se 1.4.1.
------------------------------------------------------------------------
package info.minutia.io;
import java.util.*;
import java.util.regex.*;
import java.io.File;
/**
* A thread safe file system search utility.
*
* @author Eric Noriega
* @version 0.9
*/
public class FileSearch {
File [] roots;
/**
* Creates an instance of FileSearch, with all of the system roots
* configured as search paths.
*/
public FileSearch() {
roots = File.listRoots();
}
/**
* Creates an instance of FileSearch, with the supplied name
* configured as a search path.
*
* @param root A path to begin searching from.
*
*/
public FileSearch(String root) {
if (root == null) roots = File.listRoots();
else {
roots = new File[1];
roots[0] = new File(root);
}
}
/**
* Creates an instance of FileSearch, with the supplied File
* configured as a search path.
*
* @param root A File path to begin searching from.
*
*/
public FileSearch(File root) {
if (root == null) roots = File.listRoots();
else {
roots = new File[1];
roots[0] = root;
}
}
/**
* Creates an instance of FileSearch, with the supplied Files
* configured as a search path.
*
* @param roots Array of @{link File}s, indicating where to search from.
*
*/
public FileSearch(File[] roots) {
if (roots == null) roots = File.listRoots();
else {
this.roots = new File[roots.length];
System.arraycopy(roots, 0, this.roots, 0, this.roots.length);
}
}
/**
* Initiates a search using the supplied <b>regular expression</b> string.
*
*
* @param sp A String for what to search for, expressed as a regular
expression.
* @return A List of File objects.
* @see List
* @see File
* @see Pattern
*
*/
public List search(String sp) {
return search(Pattern.compile(sp));
}
/**
* Initiates a search using the supplied <b>regular expression</b> pattern.
*
*
* @param p A regular expression Pattern indicating the file name to match.
* @return A List of File objects.
* @see List
* @see File
* @see Pattern
*/
public List search(Pattern p) {
List found = new LinkedList();
LinkedList stack = new LinkedList();
stack.addAll(Arrays.asList(roots));
while (!stack.isEmpty()) {
File currentDir = (File) stack.removeLast();
File [] listing = currentDir.listFiles();
if (listing != null) { // in case we can't get into a directory
for (int i = 0; i<listing.length; i++) {
if (p.matcher(listing[i].getName()).matches())
found.add(listing[i]);
if (listing[i].isDirectory()) stack.addLast(listing[i]);
}
}
}
return found;
}
public static void main(String [] args) {
FileSearch fs = new FileSearch("/usr");
List l = fs.search(".*\\.so");
for (Iterator i = l.iterator(); i.hasNext(); ) {
System.out.println(((File) i.next()));
}
}
}
___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html