/*
DirWriterに探索までさせているのは
DirWriterにファイルへの出力だけを任せるのなら
別に新たなクラスを作ることなくたとえばFileWriter等に任せれば事足りるから
DirListが窓口でDirWriterが実際の仕事というように分けてみた
*/
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<>();
que.add(dir);
while (! que.isEmpty()) {
dir = que.remove();
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();
}
}
}
}
LyoKRGlyV3JpdGVy44Gr5o6i57Si44G+44Gn44GV44Gb44Gm44GE44KL44Gu44GvCkRpcldyaXRlcuOBq+ODleOCoeOCpOODq+OBuOOBruWHuuWKm+OBoOOBkeOCkuS7u+OBm+OCi+OBruOBquOCiQrliKXjgavmlrDjgZ/jgarjgq/jg6njgrnjgpLkvZzjgovjgZPjgajjgarjgY/jgZ/jgajjgYjjgbBGaWxlV3JpdGVy562J44Gr5Lu744Gb44KM44Gw5LqL6Laz44KK44KL44GL44KJCkRpckxpc3TjgYznqpPlj6PjgadEaXJXcml0ZXLjgYzlrp/pmpvjga7ku5XkuovjgajjgYTjgYbjgojjgYbjgavliIbjgZHjgabjgb/jgZ8KKi8KCmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuKjsKCnB1YmxpYyBjbGFzcyBEaXJMaXN0IHsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBGaWxlTm90Rm91bmRFeGNlcHRpb24gewogICAgICAgIFNjYW5uZXIgc2MgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnQoIuS4gOimp+OCkueUn+aIkOOBmeOCi+ODh+OCo+ODrOOCr+ODiOODquWQjTogIik7CiAgICAgICAgU3RyaW5nIGRpciA9IHNjLm5leHRMaW5lKCk7CiAgICAgICAgU3lzdGVtLm91dC5wcmludCgi5Ye65Yqb44OV44Kh44Kk44Or5ZCNOiAiKTsKICAgICAgICBTdHJpbmcgb3V0ID0gc2MubmV4dExpbmUoKTsKICAgICAgICB0cnkgKERpcldyaXRlciBkdyA9IG5ldyBEaXJXcml0ZXIob3V0KSkgewogICAgICAgICAgICBkdy5saXN0KGRpcik7CiAgICAgICAgfQogICAgICAgIHNjLmNsb3NlKCk7CiAgICB9Cn0KCmNsYXNzIERpcldyaXRlciBpbXBsZW1lbnRzIEF1dG9DbG9zZWFibGUgewogICAgcHJpdmF0ZSBQcmludFdyaXRlciB3cml0ZXI7CgogICAgcHVibGljIERpcldyaXRlcihTdHJpbmcgZmlsZU5hbWUpIHRocm93cyBGaWxlTm90Rm91bmRFeGNlcHRpb24gewogICAgICAgIHdyaXRlciA9IG5ldyBQcmludFdyaXRlcihmaWxlTmFtZSk7CiAgICB9CgogICAgcHVibGljIHZvaWQgY2xvc2UoKSB7CiAgICAgICAgd3JpdGVyLmNsb3NlKCk7CiAgICB9CgogICAgcHVibGljIHZvaWQgbGlzdChTdHJpbmcgZmlsZU5hbWUpIHRocm93cyBGaWxlTm90Rm91bmRFeGNlcHRpb24gewogICAgICAgIEZpbGUgZiA9IG5ldyBGaWxlKGZpbGVOYW1lKTsKICAgICAgICBpZiAoZi5pc0RpcmVjdG9yeSgpKSB7CiAgICAgICAgICAgIGxpc3QoZik7CiAgICAgICAgfSBlbHNlIGlmIChmLmV4aXN0cygpKSB7CiAgICAgICAgICAgIHdyaXRlci5wcmludGxuKGZpbGVOYW1lKTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB0aHJvdyBuZXcgRmlsZU5vdEZvdW5kRXhjZXB0aW9uKGZpbGVOYW1lKTsKICAgICAgICB9CiAgICB9CgogICAgcHJpdmF0ZSB2b2lkIGxpc3QoRmlsZSBkaXIpIHsKICAgICAgICBRdWV1ZTxGaWxlPiBxdWUgPSBuZXcgTGlua2VkTGlzdDw+KCk7CiAgICAgICAgcXVlLmFkZChkaXIpOwogICAgICAgIHdoaWxlICghIHF1ZS5pc0VtcHR5KCkpIHsKICAgICAgICAgICAgZGlyID0gcXVlLnJlbW92ZSgpOwogICAgICAgICAgICB3cml0ZXIucHJpbnRsbigiRGlyZWN0b3J5OiAiICsgZGlyLmdldE5hbWUoKSk7CiAgICAgICAgICAgIEZpbGVbXSBkaXJsaXN0ID0gZGlyLmxpc3RGaWxlcygpOwogICAgICAgICAgICBBcnJheXMuc29ydChkaXJsaXN0LCBuZXcgQ29tcGFyYXRvcjxGaWxlPigpIHsKICAgICAgICAgICAgICAgIHB1YmxpYyBpbnQgY29tcGFyZShGaWxlIG8xLCBGaWxlIG8yKSB7CiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIG8xLmdldE5hbWUoKS5jb21wYXJlVG9JZ25vcmVDYXNlKG8yLmdldE5hbWUoKSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0pOwogICAgICAgICAgICBmb3IgKEZpbGUgZiA6IGRpcmxpc3QpIHsKICAgICAgICAgICAgICAgIHdyaXRlci5wcmludChmLmdldE5hbWUoKSk7CiAgICAgICAgICAgICAgICBpZiAoZi5pc0RpcmVjdG9yeSgpKSB7CiAgICAgICAgICAgICAgICAgICAgd3JpdGVyLnByaW50KCIg44Gv44OH44Kj44Os44Kv44OI44Oq44Gn44GZ44CCIik7CiAgICAgICAgICAgICAgICAgICAgcXVlLmFkZChmKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHdyaXRlci5wcmludGxuKCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0K