List all files in the classpath or in a JarTag(s): IO


List the files found in the classpath, the bootclasspath, the extension directory and in a Jar.
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


public class FileUtils {

  /**
   * list files in the given directory and subdirs (with recursion)
   * @param paths
   * @return
   */
  public static List<File> getFiles(String paths) {
    List<File> filesList = new ArrayList<File>();
    for (final String path : paths.split(File.pathSeparator)) {
      final File file = new File(path);
      if( file.isDirectory()) {
         recurse(filesList, file);
      }
      else {
        filesList.add(file);
      }
    }
    return filesList;
  }

  private static void recurse(List<File> filesList, File f) { 
    File list[] = f.listFiles();
    for (File file : list) {
      if (file.isDirectory()) {
         recurse(filesList, file);
      }
      else {
        filesList.add(file);
      }
    }
  }
  
  /**
   * List the content of the given jar
   * @param jarPath
   * @return
   * @throws IOException
   */
  public static List<String> getJarContent(String jarPath) throws IOException{
    List<String> content = new ArrayList<String>();
    JarFile jarFile = new JarFile(jarPath);
    Enumeration<JarEntry> e = jarFile.entries();
    while (e.hasMoreElements()) {
      JarEntry entry = (JarEntry)e.nextElement();
      String name = entry.getName();
      content.add(name);
    }
    return content;
  }
  
  
  public static void main(String args[]) throws Exception {
    List<File> list = FileUtils.getFiles(System.getProperty("java.class.path"));
    for (File file: list) {
      System.out.println(file.getPath());
    }
    
    list = FileUtils.getFiles(System.getProperty("sun.boot.class.path"));
    for (File file: list) {
      System.out.println(file.getPath());
    }
    list = FileUtils.getFiles(System.getProperty("java.ext.dirs"));
    for (File file: list) {
      System.out.println(file.getPath());
    }
    
    List<String> content = FileUtils.getJarContent("c:/temp/DirWatch.jar");
    for (String file: content) {
      System.out.println(file);
    }
    
  }  
}

blog comments powered by Disqus