title: How can I count the number of files in a folder within a JAR?
body: <p>I've spent a bit of time trying to find a way to count the number of files in a folder within a JAR. I put together several examples of code that served different purposes to make this work. It counts just fine when I run the code through Eclipse but after exporting to a JAR it fails and returns 0. In this case, my folder path I use is just "rules/". I would appreciate any recommendations or samples. Thanks.</p>
  
  <pre><code>public static int countFiles(String folderPath) throws IOException { //Counts the number of files in a specified folder
      ClassLoader loader = ToolSet.class.getClassLoader();
      InputStream is = loader.getResourceAsStream(folderPath);
      try {
          byte[] c = new byte[1024];
          int count = 0;
          int readChars = 0;
          boolean empty = true;
          while ((readChars = is.read(c)) != -1) {
              empty = false;
              for (int i = 0; i &lt; readChars; ++i) {
                  if (c[i] == '\n') {
                      ++count;
                  }
              }
          }
          return (count == 0 &amp;&amp; !empty) ? 1 : count;
      } finally {
          is.close();
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  The following doesn't exactly match my original question but thanks to MadProgrammer I was able to reduce my code and eliminate the need to even count the files. The code blow searches every file in my JAR looking for those that end with ".rules", opens the file, searches the file for a string that matches "searchBox.getText()", appends results, and continues on to the next ".rules" file.</p>
  
  <pre><code>    StringBuilder results = new StringBuilder();
      int count = 0;
      JarFile jf = null;
      try {
          String path = ToolSet.class.getProtectionDomain().getCodeSource().getLocation().getPath();
          String decodedPath = URLDecoder.decode(path, "UTF-8");
          jf = new JarFile(new File(decodedPath));
          Enumeration&lt;JarEntry&gt; entries = jf.entries();
          while (entries.hasMoreElements()) {
              JarEntry entry = entries.nextElement();
              if (entry.getName().endsWith(".rules")) {
                  String name = entry.getName();
                  InputStream in = ToolSet.class.getResourceAsStream(name);
                  InputStreamReader isr = new InputStreamReader(in);
                  BufferedReader bf = new BufferedReader(isr);
                  String line;
                  while ((line = bf.readLine()) != null) {
                      String lowerText = line.toLowerCase();
                      if(lowerText.indexOf(searchBox.getText().toLowerCase()) &gt; 0) {
                          results.append(line + "\n");
                          count++;
                      }
                  }
                  bf.close();
              }
          }
      } catch (IOException ex) {
          try {
              jf.close();
          } catch (Exception e2) {
          }
      }
      if(count&gt;0) {
          logBox.setText(results.toString());
      } else {
          logBox.setText("No matches could be found");
      }
  </code></pre>