title: How to detect if a file(with any extension) exist in java
body: <p>I am searching for a sound file in a folder and want to know if the sound file exist may it be .mp3,.mp4,etc.I just want to make sure that the filename(without extension) exists.</p>
  
  <p>eg.File searching /home/user/desktop/sound/a</p>
  
  <p>return found if any of a.mp3 or a.mp4 or a.txt etc. exist.</p>
  
  <p>I tried this:</p>
  
  <pre><code>File f=new File(fileLocationWithExtension);
  
  if(f.exist())
     return true;
  else return false;
  </code></pre>
  
  <p>But here I have to pass the extension also otherwise its returning false always</p>
  
  <p>To anyone who come here,this is the best way I figured out</p>
  
  <pre><code>    public static void main(String[] args) {
      File directory=new File(your directory location);//here /home/user/desktop/sound/
      final String name=yourFileName;  //here a;
              String[] myFiles = directory.list(new FilenameFilter() {
                  public boolean accept(File directory, String fileName) {
                      if(fileName.lastIndexOf(".")==-1) return false;
                      if((fileName.substring(0, fileName.lastIndexOf("."))).equals(name))
                          return true;
                      else return false;
                  }
              });
     if(myFiles.length()&gt;0)
         System.Out.println("the file Exist");
  }
  </code></pre>
  
  <p><strong>Disadvantage</strong>:It will continue on searching even if the file is found which I never intended in my question.Any suggestion is welcome</p>