问题重现:
删除某个路径下特定类型的文件,比如class类型文件,比如txt类型文件
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* 删除某个路径下特定类型的文件,比如class类型文件,比如txt类型文件
*
*/
public class DeleteFileByFileType {
public static void main(String[] args) throws Exception {
/**
* 初始化configuration
*/
String url = "hdfs://hadoop01:9000";
Configuration conf = new Configuration();
conf.set("fs.defaultFS", url);
System.setProperty("HADOOP_USER_NAME", "hadoop");
/**
* 获取文件系统
*/
FileSystem fs = FileSystem.get(conf);
/**
* 从本地上传文件到指定的hdfs目录
*/
//fs.copyFromLocalFile(new Path("g:/files/zhangsan.txt"), new Path("/aa/bb"));
//fs.copyFromLocalFile(new Path("g:/files/lisi.pdf"), new Path("/aa/bb"));
/**
* 遍历指定文件下的所有的文件
*/
FileStatus fileStatus[] = fs.listStatus(new Path("/aa/bb"));
String fileName = "";
int startIndex,endIndex;
String FILETYPE="pdf";
String path = "";
for (FileStatus file : fileStatus) {
//查看文件的详情
/*System.out.println(file.getAccessTime());
System.out.println(file.getBlockSize());
System.out.println(file.getGroup());
System.out.println(file.getLen());
System.out.println(file.getModificationTime());
System.out.println(file.getOwner());
System.out.println(file.getPath());
System.out.println(file.getPermission());
System.out.println(file.getReplication());
//System.out.println(file.getSymlink());
System.out.println(file.getPath().getName());
System.out.println(file.getPath().getParent());
*/
/*
1520114750420 getAccessTime()
134217728 getBlockSize()
supergroup getGroup()
27 getLen()
1520114751189 getModificationTime()
hadoop getOwner()
hdfs://hadoop01:9000/aa/bb/gg.class getPath()
rw-r--r-- getPermission()
2 getReplication()
gg.class file.getPath().getName()
hdfs://hadoop01:9000/aa/bb getPath().getParent()
*/
//获取文件名称 然后判断文件的类型
fileName = file.getPath().getName();
startIndex = fileName.length()-FILETYPE.length();
endIndex = fileName.length();
System.out.println(fileName.substring(startIndex,endIndex));
System.out.println(FILETYPE.equals(fileName.substring(startIndex,endIndex)));
if(FILETYPE.equals(fileName.substring(startIndex,endIndex))){
path = file.getPath().toString().substring(url.length(),file.getPath().toString().length());
System.out.println(path);
fs.delete(new Path(path),true);
}
}
fs.close();
}
}