/**
* 递归遍历打印HDFS的目录树
* @param fs FileSystem
* @param path HDFS存储路径
* @throws IOException
*/
public static void iteratorShowFiles(FileSystem fs, Path path) throws IOException {
if (fs == null || path == null) return;
FileStatus[] listStatus = fs.listStatus(path);
for (FileStatus fileStatus : listStatus) {
if (fileStatus.isDirectory()) {
System.out.println(depthString(fileStatus.getPath().depth(),">") + fileStatus.getPath().getName());
iteratorShowFiles(fs, fileStatus.getPath());
} else {
System.out.println(depthString(fileStatus.getPath().depth(),"|") + fileStatus.getPath().getName());
}
}
}
/**
* 打印层级深度的字符串数量
* @param dept 深度
* @param s 文件夹:> 文件: |
* @return
*/
public static String depthString(int dept,String s){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < dept; i++) {
sb.append(s);
}
return sb.toString();
}
运行效果:
>idea
||a1.txt
|merge.zip
>sanguo
>>shuguo
|||README.txt
|||cp.txt