I am trying to create a File Searching Utility that finds all Files with a given Name which searches all Folders (Sub Directories, and Sub Directories of Sub Directories) in a given Directory. I was thinking of a way to use For Loops, or For Each, but that would mean I would need a For Loop or a For Each, for each sub directory. Basically output a line for each file found.
Example:
Input File Name: Hello.txt
Input Inital Directory: C:/Users/GeoDoX/Documents/
File: Hello.txt
Path: C:\Users\GeoDoX\Documents\
C:\Users\GeoDoX\Documents\Hello.txt
C:\Users\GeoDoX\Documents\Test\Hello.txt
C:\Users\GeoDoX\Documents\Test\Hello\Hello.txt
Obviously there is some recursion, and I can't think of where to start, or even how to get a list of directories in the current directory.
PS: I want this to support Wild Cards (.txt, *.pdf, etc, as well as (name)., but not .)
Here's what I have so far though.
package com.github.geodox.filefinder.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* @author GeoDoX
*
* FileFinder.java
*/
public class FileFinder
{
public static void main(String[] args)
{
new FileFinder();
}
private FileFinder()
{
InputStreamReader reader = new InputStreamReader(System.in); /* Needed for getting Input from User, Passed to BufferedReader */
BufferedReader input = new BufferedReader(reader); /* Gets user input */
File file = null; /* File being Searched for, Can be wild card */
Path dir = null; /* Directory for searching for the file(s) requested in this Path, and Sub Paths */
try
{
/* Get File */
log("Input File Name: ");
file = new File(input.readLine());
/* Get Directory */
log("Input Inital Directory: ");
dir = Paths.get(input.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
outputFileAndPath(file, dir);
}
private void outputFileAndPath(File file, Path dir)
{
/* Print the File and Path info */
log("\n");
log("File: " + file);
log("\n");
log("Path: " + dir);
}
private void log(Object aObject)
{
/* Simple method for printing */
System.out.print(aObject);
}
private void logln(Object aObject)
{
/* Simple method for printing a line */
System.out.println(aObject);
}
}
Sample Input and Output:
Input File Name: Hello.txt
Input Inital Directory: C:/Users/GeoDoX/Desktop/
File: Hello.txt
Path: C:\Users\GeoDoX\Desktop