title: How to loop through a directory, get all file names, then get the contents of those files in Java
body: <p>So in C drive, I have a folder called <code>Search Files</code> and inside <code>Search Files</code>, I have four subdirectories called <code>Folder 1</code>, <code>Folder 2</code>, <code>Folder 3</code>, and <code>Folder 4</code>.</p>
  <p>Inside of <code>Folder 1</code>, I have a text file called <code>hello.txt</code> and the contents of that file is <code>hello</code>.</p>
  <p>My expected output is <code>&quot;Directory of the file&quot; + &quot;file name&quot; + &quot;file body&quot;</code>.</p>
  <p>Below is the code that I have right now.</p>
  <pre><code>import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.Iterator;
  
  import org.apache.commons.io.FileUtils;
  
  public class FileDirectories {
  
      public static void main(String[] args) throws IOException {
          File[] files = new File(&quot;C:\\Search Files&quot;).listFiles();
          showFiles(files);
      }
  
      public static void showFiles(File[] files) throws IOException {
          String line = null;
          
              try{
                  for (File file : files) {
                      if (file.isDirectory()) {
                          String fileName = &quot;Directory&quot; + file.getName();
                          //System.out.println(&quot;Directory: &quot; + file.getName());
                          BufferedReader in = new BufferedReader(new FileReader(file));
                          while((line = in.readLine()) != null)
                          {
                              System.out.println(line);
                          }
                          in.close();
                          showFiles(file.listFiles()); // Calls same method again.
                      } else {
                          System.out.println(&quot;File: &quot; + file.getName() + file.toString());
                      }
                  }
              }catch(NullPointerException e){
                  e.printStackTrace();
              }
          
          
          
          
          /*Iterator&lt;File&gt; it = FileUtils.iterateFiles(new File(&quot;C://Search Files//&quot;), null, false);
          while(it.hasNext()) {
              System.out.println(((File) it.next()).getName());
          }*/
      }
  }
  </code></pre>
  <p>When I execute the above code, I get the following error:</p>
  <pre><code> Exception in thread &quot;main&quot; java.io.FileNotFoundException: C:\Search   Files\Folder 1 (Access is denied)
          at java.io.FileInputStream.open(Native Method)
          at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:146)
          at java.io.FileReader.&lt;init&gt;(FileReader.java:72)
          at org.raghav.stuff.FileDirectories.showFiles(FileDirectories.java:27)
          at org.raghav.stuff.FileDirectories.main(FileDirectories.java:16)
  </code></pre>
  <p>Once again, I need to get the file directory, the file name, and the contents of the file.</p>
  <p>In case of the <code>hello.txt</code>, the expected output should be:
  <strong>C:\Search Files\Folder1\   hello.txt   hello</strong></p>
  <p>Can you guys point me in the right directions? How to fix the above exception and how do I get a String that displays the directory, file name, and the contents of the file?</p>