欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

删除特定文件(.txt)

程序员文章站 2022-04-17 11:29:58
...
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);
            }
        }

    }

}