/*
課題で求められているDirListとDirWriterの分担の詳細が分からなかったので
DirListが条件を読み込んだ後DirWriterに条件を渡して仕事を任せ
DirWriterがディレクトリツリーの探索とファイルへの書き込みを行っている
ファイルリストは実行例での表示順が大文字小文字無視の並べ替え後のようなので
Arrays.sort()でそうなるように並べ替えている
不要ならsortを通さずにそのままdirlistをfor文に使えばよい
*/
import java.io.*;
import java.util.*;
public class DirList {
Scanner sc
= new Scanner
(System.
in); System.
out.
print("一覧を生成するディレクトリ名: "); System.
out.
print("出力ファイル名: "); try (DirWriter dw = new DirWriter(out)) {
dw.list(dir);
}
sc.close();
}
}
class DirWriter implements AutoCloseable {
}
public void close() {
writer.close();
}
if (f.isDirectory()) {
list(f);
} else if (f.exists()) {
writer.println(fileName);
} else {
}
}
private void list
(File dir
) { Queue<File> que = new LinkedList<>();
writer.println("Directory: " + dir.getName());
File[] dirlist
= dir.
listFiles(); Arrays.
sort(dirlist,
new Comparator
<File
>() { return o1.getName().compareToIgnoreCase(o2.getName());
}
});
writer.print(f.getName());
if (f.isDirectory()) {
writer.print(" はディレクトリです。");
que.add(f);
}
writer.println();
}
while (! que.isEmpty()) list(que.remove());
}
}
LyoK6Kqy6aGM44Gn5rGC44KB44KJ44KM44Gm44GE44KLRGlyTGlzdOOBqERpcldyaXRlcuOBruWIhuaLheOBruips+e0sOOBjOWIhuOBi+OCieOBquOBi+OBo+OBn+OBruOBpwpEaXJMaXN044GM5p2h5Lu244KS6Kqt44G/6L6844KT44Gg5b6MRGlyV3JpdGVy44Gr5p2h5Lu244KS5rih44GX44Gm5LuV5LqL44KS5Lu744GbCkRpcldyaXRlcuOBjOODh+OCo+ODrOOCr+ODiOODquODhOODquODvOOBruaOoue0ouOBqOODleOCoeOCpOODq+OBuOOBruabuOOBjei+vOOBv+OCkuihjOOBo+OBpuOBhOOCiwrjg5XjgqHjgqTjg6vjg6rjgrnjg4jjga/lrp/ooYzkvovjgafjga7ooajnpLrpoIbjgYzlpKfmloflrZflsI/mloflrZfnhK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3cml0ZXIucHJpbnQoZi5nZXROYW1lKCkpOwogICAgICAgICAgICBpZiAoZi5pc0RpcmVjdG9yeSgpKSB7CiAgICAgICAgICAgICAgICB3cml0ZXIucHJpbnQoIiDjga/jg4fjgqPjg6zjgq/jg4jjg6rjgafjgZnjgIIiKTsKICAgICAgICAgICAgICAgIHF1ZS5hZGQoZik7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd3JpdGVyLnByaW50bG4oKTsKICAgICAgICB9CiAgICAgICAgd2hpbGUgKCEgcXVlLmlzRW1wdHkoKSkgbGlzdChxdWUucmVtb3ZlKCkpOwogICAgfQp9Cg==