import javax.swing.JFileChooser; import java.io.File; import java.util.Scanner; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; /** * Searches for filename matches on your disk. * * @author Brad Richards * @version 1.1 */ public class FileSearch { // We could "print" to this output window instead of out // if we wanted to make this a standalone application. private static TextDisplay out = new TextDisplay(); /** * Bring up a dialog box and let the user select a directory. If * they cancel out without selecting a valid directory, this method * returns null. * * @return A File object corresponding to the selected directory, or null. */ public static File getDirectory() { File selected = null; JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle("Please select a directory to search"); int result = fileChooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { selected = fileChooser.getSelectedFile(); } return selected; } /** * Looks through the contents of the specified directory and prints * information about files whose names contain the specified search term. * * @param directory File object representing the directory to be searched * @param key A string containing text we're looking for */ public static void listMatches(File directory, String key) { if (directory != null && directory.isDirectory()) { File[] contents = directory.listFiles(); // Get contents of directory // out.println("Directory contains "+contents.length+" files"); // out.println("First file's name is "+contents[0].getName()); for(File oneFile : contents) { if (oneFile.isDirectory()) { out.println("Searching subdirectory: "+oneFile.getName()); listMatches(oneFile, key); } else { if (oneFile.getName().contains(key)) { out.println(oneFile.getName()); } } } } } /** * This pulls the pieces together: It prompts the user for some search * text, calls getDirectory() so the user can select a directory to search, * and prints "Done" once the search finishes so the user knows it's done. */ public static void runSearch() { String key = JOptionPane.showInputDialog("Search for what?"); // Have user select directory File directory = getDirectory(); if (directory != null) { out.println("Searching "+directory.getAbsolutePath()); // Pass directory and search key info to listMatches listMatches(directory, key); // Print "done". out.println("Done."); } else { out.println("Didn't select a directory."); } } /** * This is really gross, and you don't need to understand it. The issue * is that our program pulls up dialog boxes like JFileChooser, and those * are officially supposed to be created on a separate "thread" or they * can deadlock. (In English: Our program can "hang" without making any * progress if we don't let it run in its own personal "space".) The code * below creates a separate thread and starts runSearch() running in that * thread. */ public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { runSearch(); } }); } }