* 删除某一路径
* @param path 需要删除的路径
* @param recursive 指定为true删除目录中全部文件,false时可以删除空目录和单个文件
* @return
*/
public boolean delete(String path, boolean recursive) {
boolean result = false;
if(recursive) {
try {
result = fs.delete(new Path(path), true);
} catch (Exception e) {
e.printStackTrace();
result = false;
}
return result;
}else{
try {
result = fs.delete(new Path(path), false);
} catch (Exception e) {
e.printStackTrace();
result = false;
}
return result;
}
}
获取某一路径下的文件信息/**
* 获得某一路径下的文件信息
* @param path 待查看路径
* @return 文件信息列表-包含文件类型,文件大小,所有者,所在组,文件名称
*/
public List<String> getFileInfo(String path){
List<String> infos = new ArrayList<>();
try {
// 通过FileSystem获得某一路径下的文件状态列表
FileStatus[] fileStatus = fs.listStatus(new Path(path));
for (FileStatus temp : fileStatus) {
String info = "";
// 判断文件类型
if (temp.isDirectory()) {
info += "目录\t" + "0" + "\t";
}else {
info += "文件\t" + sizeFormat(temp.getLen()) + "\t";
}
// 拼接文件信息
info += temp.getOwner() + "\t" + temp.getGroup() + "\t" + temp.getPath().getName();