What I need to do is take a path to search for a file from the user and also take a set of characters from the user which is a part of the filename to be searched. Once the file is found then it would display all the locations including sub-folders along with complete filename.What I have done is taken all the files in the given path and put them in a File[] array using ListFiles().Getting the names of all the files and save them into an arraylist. Make another arraylist and save only the filename without the extension from the previous arraylist. Then use the set of characters entered by the user to compare with each filename in this arraylist.To test if this much was working I printed it out but its printing all the file names and not just the ones that match the character set the user entered.Here is my code:
import java.util.*;
import java.io.*;
public class FileSearch
{
public static void main(String args[])
{
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(" 1.Enter the path to search:");
String str = br.readLine();
System.out.println(" 2.Enter some characters of the filename:");
String str1 = br.readLine();
File files = new File(str);
File[] f = files.listFiles();
List<String> li = new ArrayList<String>();
List<String> li1 = new ArrayList<String>();
for(int i=0;i<f.length;i++)
{
if(f[i].isFile())
{
li.add(f[i].getName());
}
}
for(int i=0;i<li.size();i++)
{
int j = li.get(i).lastIndexOf('.');
li1.add((j>-1)?li.get(i).substring(0, j):li.get(i));
}
for(int i=0;i<li1.size();i++)
{
if(li1.get(i).contains(str1));
{
System.out.println(li1.get(i));
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}