#author("2017-07-15T00:35:42+09:00","","")
#author("2017-07-15T14:09:52+09:00","","")
#navi(../)

&color(red){サンプルソースバグってます!修正中です…};

* サブディレクトリ内を含むファイル一覧を取得するサンプル [#r2bbe61d]
Javaで指定したディレクトリ内のファイル一覧を取得するサンプルソースです。~
再帰呼出しにより、サブディレクトリ内のファイル一覧も取得します。

#contents

* サンプルソース [#hd40c940]
getFileList()メソッドを再帰呼出しすることにより、サブディレクトリ内のファイル一覧を取得しています。~
尚、本サンプルプログラムは以下の引数を必要とします。
 java RecursiveFileList ディレクトリ [サブディレクトリの深さ・省略可能]
使用例として以下のようになります。
 java RecursiveFileList . 1
上記の例では、カレントディレクトリで深さ=1なので、サブディレクトリを検索しません。~
(lsやdirをオプションなしで実行した時と同じ動作になります。)~
 java RecursiveFileList .
深さを指定しないと、カレントディレクトリ以下のファイルやディレクトリすべてを表示します。~
 java RecursiveFileList . 2
カレントディレクトリとカレントディレクトリ内のサブフォルダ内ファイルまでが対象となります。

&ref(RecursiveFileList.java); LF
 import java.io.File;
 import java.util.List;
 import java.util.ArrayList;
 
 class RecursiveFileList {
     int recursiveCount;
     int depth;
     String directory;
 
     private static void usage() {
         System.err.println("Usage: java RecursiveFileList <path> [depth]");
         System.exit(1);
     }
 
     private static boolean isNum(String v) {
         boolean b = true;
         try {
             Integer.parseInt(v);
         } catch (NumberFormatException e) {
             b = false;
         }
         return b;
     }
 
     private int getDepth() {
         return this.depth;
     }
 
     private void setDepth(int d) {
         this.depth = d;
     }
 
     private void setDirectory(String d) {
         this.directory = d;
     }
 
     private String getDirectory() {
         return this.directory;
     }
 
     private List<File> getFileList(String dir, List<File> fileList) {
         recursiveCount++;
         File f = new File(dir);
         File[] fl = f.listFiles();
         for (File item : fl) {
             fileList.add(item);
             if (item.isDirectory()) {
                 if ((getDepth() > recursiveCount) || (getDepth() == 0)) {
                     getFileList(dir + System.getProperty("file.separator") + item.getName(), fileList);
                 }
             }
         }
         return fileList;
     }
 
     RecursiveFileList(String dir, int depth) {
         setDepth(depth);
         setDirectory(dir);
         recursiveCount = 0;
     }
 
     public static void main(String[] args) {
         int d = 0;
         // check arguemnts
         if ((args.length !=1) && (args.length !=2)) {
             usage();
         }
         // check numeric
         if (args.length == 2) {
             if (!isNum(args[1])) {
                 System.err.println(args[1] +" is not numeric.");
                 usage();
             }
             if (Integer.parseInt(args[1]) < 0) {
                 System.err.println("Please enter a number greater than 1.");
                 usage();
             }
             d = Integer.parseInt(args[1]); // recursive depth
         }
         // directory ?
         File f = new File(args[0]);
         if (!f.exists()) {  // false
             System.out.println(args[0] + " directory is not found.");
             System.exit(2);
         }
         if (f.isFile()) {
             System.out.println(args[0] + " is file, Please set the directory.");
             System.exit(3);
         }
         if(!f.isDirectory()) {
             System.out.println(args[0] + " is unknown, Please set the directory.");
             System.exit(4);
         }
 
         RecursiveFileList me = new RecursiveFileList(args[0], d);
         List<File> fileList = new ArrayList<File>();
         me.getFileList(args[0], fileList);
         for (File fitem : fileList) {
             System.out.println(fitem.getPath());
         }
     }
 }


* 実行結果 [#h8e81dc0]
コンパイルして実行してみます。
- コンパイルします。
 $ javac RecursiveFileList.java 
+ 引数を指定しないと以下のようにUsageが表示されます。
 $ java RecursiveFileList 
 Usage: java RecursiveFileList <path> [depth]
+テスト用のディレクトリを作成します。
 $ mkdir -p dirA1/{dirA2-1,dirA2-2/dirA3} dirB1
 $ tree 
 .
 ├── RecursiveFileList.class
 ├── RecursiveFileList.java
 ├── dirA1
 │   ├── dirA2-1
 │   └── dirA2-2
 │       └── dirA3
 └── dirB1
 
 5 directories, 2 files
+ テスト用のファイルを作成します。
 $ touch dirA1/fileA1 dirA1/dirA2-1/fileA2-1 dirA1/dirA2-2/dirA3/{fileA3-1,filea3-2}
 $ tree 
 .
 ├── RecursiveFileList.class
 ├── RecursiveFileList.java
 ├── dirA1
 │   ├── dirA2-1
 │   │   └── fileA2-1
 │   ├── dirA2-2
 │   │   └── dirA3
 │   │       ├── fileA3-1
 │   │       └── filea3-2
 │   └── fileA1
 └── dirB1
 
 5 directories, 6 files
+ 実行してみます。
 $ java RecursiveFileList .
 ./RecursiveFileList.class
 ./dirA1
 ./dirA1/dirA2-1
 ./dirA1/dirA2-1/fileA2-1
 ./dirA1/fileA1
 ./dirA1/dirA2-2
 ./dirA1/dirA2-2/dirA3
 ./dirA1/dirA2-2/dirA3/fileA3-1
 ./dirA1/dirA2-2/dirA3/filea3-2
 ./RecursiveFileList.java
 ./dirB1
#br
 $ java RecursiveFileList . 1
 ./RecursiveFileList.class
 ./dirA1
 ./RecursiveFileList.java
 ./dirB1
#br
 $ java RecursiveFileList . 2
 ./RecursiveFileList.class
 ./dirA1
 ./dirA1/dirA2-1
 ./dirA1/fileA1
 ./dirA1/dirA2-2
 ./RecursiveFileList.java
 ./dirB1

以上、再帰呼出を使った指定したディレクトリ以下サブディレクトリ内ファイルを含む~
ファイルおよびディレクトリ一覧を表示するサンプルソースでした。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS