title: Arraylist and files
body: <p>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 <strong>File[]</strong> array using <strong>ListFiles()</strong>.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:</p>
  
  <pre><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&lt;String&gt; li = new ArrayList&lt;String&gt;();
          List&lt;String&gt; li1 = new ArrayList&lt;String&gt;();
  
          for(int i=0;i&lt;f.length;i++)
          {
              if(f[i].isFile())
              {
                  li.add(f[i].getName());
              }
          }
  
          for(int i=0;i&lt;li.size();i++)
          {
             int j = li.get(i).lastIndexOf('.');
             li1.add((j&gt;-1)?li.get(i).substring(0, j):li.get(i));
          }
  
          for(int i=0;i&lt;li1.size();i++)
           {
              if(li1.get(i).contains(str1));
              {
                  System.out.println(li1.get(i));
              }
          }
       }
      catch(Exception e)
       {
          e.printStackTrace();
       }
    }
   }
  </code></pre>