删除特定文件(.txt)

package com.ghgj.cn.zy;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;

public class deleteClass {
//  删除特定类型的文件
    public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop01:9000"), conf, "hadoop");
        RemoteIterator<LocatedFileStatus> lf = fs.listFiles(new Path("/tt/aa"),true);

        while(lf.hasNext()){
            LocatedFileStatus next = lf.next();
            Path p = next.getPath();
            String name = next.getPath().getName();
            if(name.endsWith(".txt")){
                fs.delete(p, false);
            }
        }

    }

}

猜你喜欢

转载自blog.csdn.net/YZY_001/article/details/82078215