title: Read files from sub directories of class path resource folder in spring boot
body: <p>I would like to read files from sub directories of resource folder.
  I am facing issues with jar execution.</p>
  
  <p>This is my directory structure.</p>
  
  <p><code>src/main/resources
      |_ Conf
           |_ conf1
                |_ config.txt
           |_ conf2
                |_ config.txt</code></p>
  
  <p>Here, I am trying to read <code>config.txt</code> files from all sub directories of <code>Conf</code> folder.  I do not know what sub directories <code>Conf</code> will have. I know the classpath till <code>Conf</code>. So, I will give classpath till <code>Conf</code> and trying to get sub directories and files.</p>
  
  <p>I tried to achieve this using <code>ClassPathResource</code>. This works fine if it is file. I am facing issues when it comes to directory. I am using <code>getFile</code> api to get the directory path to walk through that directory for sub directories which is causing issue in jar execution.</p>
  
  <p>Here is my code:</p>
  
  <p>Below code is to read sub directories in <code>Conf</code> folder.</p>
  
  <pre><code>List&lt;Map&lt;String,String&gt;&gt; list = new ArrayList&lt;Map&lt;String,String&gt;&gt;();
   ClassPathResource classPathResource = new ClassPathResource("Conf");
   File dir = classPathResource.getFile();
   Files.walk(Paths.get(dir.toString()))
       .filter(Files::isDirectory)
        // This is to exempt current dir.
       .filter((Path p)-&gt;!p.toString().equals(dir.toString()))
       .forEach(f-&gt; {list.add(readDirectory(f.toString()));});
  </code></pre>
  
  <p>Reading each sub directory.</p>
  
  <pre><code>public Map&lt;String, String&gt; readDirectory(String dir) {
       Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
       String confDir = dir.substring(dir.lastIndexOf(File.separator)+1);
      try {
            Files.list(Paths.get(dir))
                     .filter(f-&gt;f.toString().matches(".*conf\\.txt"))
             .forEach(file -&gt;approvedTermsMap.put
                                 (confDir,readFile(file.toFile())));
      } catch (IOException e) {
          e.printStackTrace();
          }
          return map;
   }
  </code></pre>
  
  <p>Reading file:</p>
  
  <pre><code>public String readFile(File confFile) {
  
         StringBuffer terms = new StringBuffer();
         try (BufferedReader reader = new BufferedReader(new 
               FileReader(confFile)))
      {
          reader.lines().forEach(term-&gt;                    
                               terms.append(term + "|"));
      } catch (FileNotFoundException e) {
              e.printStackTrace();
      } catch (IOException e) {
              e.printStackTrace();
      }
  return terms.toString();
  }
  </code></pre>
  
  <p>Here, I should not use <code>classPathResource.getFile()</code> to get the absolute path because it tries to find file in file system which will not avilable in case of jar. So, I need alternate way to get absolute path of resource directory. I have to pass it to <code>File.walk</code> api to find sub directories and files.</p>