Get the content of a directory with subdirectoriesTag(s): IO


import java.io.*;
import java.util.*;
public class DirUtils {

  public static List recurseDir(String dir) {
      String result, _result[];

      result = recurseInDirFrom(dir);
      _result = result.split("\\|");
      return Arrays.asList(_result);
  }

  private static String recurseInDirFrom(String dirItem) {
    File file;
    String list[], result;

    result = dirItem;

    file = new File(dirItem);
    if (file.isDirectory()) {
      list = file.list();
      for (int i = 0; i < list.length; i++)
        result = result + "|" 
           + recurseInDirFrom(dirItem + File.separatorChar + list[i]);
      }
    return result;
  }

  public static void main(String arg[]) {
    if (arg.length > 0) {
      System.out.println("recursive Dirs from " + arg[0]);
      System.out.println(DirUtils.recurseDir(arg[0]));
    }
    else {
      System.out.println("Usage :");
      System.out.println("  java DirUtils c:\temp");
    }
    
    /*
    output:
    
    C:\Applications>java DirUtils c:\zip
    recursive Dir from c:\zip
    [c:\zip, c:\zip\AutoIt.exe, c:\zip\cc32d48.exe, c:\zip\CODE_UPLOAD
    148812122001.zip, c:\zip\groups2.txt, c:\zip\httrack-3.23.exe, c:\
    zip\mass20.zip, c:\zip\mp3,c:\zip\mp3\BonkEnc-1.0-beta1.exe, c:\zi
    p\mp3\cdex_150b10_enu.exe, c:\zip\mp3\hh_install.exe, c:\zip\mp3\T
    PlayerSetup.exe, c:\zip\mp3\Tunchy1017full.zip, c:\zip\mp3\TWMP3en
    coder.exe, c:\zip\ok, c:\zip\ok\CmdHerePowertoySetup.exe, c:\zip\o
    k\htmldoc-1.8.23-winfree.exe, c:\zip\ok\j2sdk-1_4_1_02-windows-i58
    6.exe, c:\zip\ok\jdk141-hh.zip, c:\zip\ok\jsref13.zip, c:\zip\ok\T
    weakUiPowertoySetup.exe, c:\zip\quickmail-2_1.zip, c:\zip\RealOneP
    layerV2GOLD.exe, c:\zip\RESTorefile2514.EXE,c:\zip\Shockwave_Insta
    ller_Full.exe, c:\zip\Water.exe, c:\zip\winamp50_full.exe,c:\zip\w
    olf, c:\zip\wolf\ok, c:\zip\wolf\ok\kf_box.zip, c:\zip\wolf\sp_off
    lag_levels_1-4_final.zip, c:\zip\xpAntiSpy.zip]
    */
  }
}

blog comments powered by Disqus