private static List<String>list = new ArrayList<String>()
private static int floor = 0
public static void printDirectory(File root) {
if (root.isFile()) {
System.out.println(getPreString(floor, root) + root.getName())
} else {
System.out.println(getPreString(floor, root) + root.getName())
floor++
File[] files = root.listFiles()
setLast(files)
for (File file : files) {
printDirectory(file)
}
floor--
}
}
private static String getPreString(int floor, File file) {
if (floor == 0)
return ""
String preString = "ㄴ"
for (int i = 1i <floori++) {
file = file.getParentFile()
if (list.contains((file.getAbsolutePath() + file.getName())))
preString = " " + preString
else
preString = "|" + preString
}
return preString
}
private static void setLast(File[] files) {
if (files.length != 0) {
int last = 0
for (int i = 0i <files.lengthi++) {
if (files[i].isDirectory())
last = i
}
list.add(files[last].getAbsolutePath() + files[last].getName())
}
}
public static void main(String[] args) {
DirectoryPrinter.printDirectory(new File("D://workspace//JavaTest"))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)