自己写递归,实现HDFS上的某个文件夹的级联删除,(将delete的第二个参数该为false,自己手动实现级联删除的功能)
package com.zzm.day03;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
public class TestDelete {
public static void main(String[] args) throws Exception {
Path path = new Path("/zzm");
delete(path);
}
public static void delete(Path path) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
FileStatus[] files = fs.listStatus(path);
if (files.length == 0) {
fs.delete(path, false);
} else {
for (FileStatus fst : files) {
if (fst.isFile()) {
fs.delete(path, false);
} else {
delete(fst.getPath());
}
}
}
fs.delete(path, false);
}
}