hadoop 递归列出文件

@Test
public void listFiles() throws Exception {


// listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path(
"hdfs://hadoop0:9000/flow"), true);

while (files.hasNext()) {
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}


System.out.println("---------------------------------");

// listStatus 可以列出文件和文件夹的信息,但是不提供自带的递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("/"));
recuersionFile(listStatus);


}


public void recuersionFile(FileStatus[] listStatus) throws Exception {
if (listStatus != null) {
for (FileStatus status : listStatus) {

if (status.isDirectory()) {
FileStatus[] listStatus2 = fs.listStatus(new Path(status.getPath().toString()));
recuersionFile(listStatus2);
}else{
String name = status.getPath().getName();
// System.out.println(status.getPath());
System.out.println("fileName: "+name);
}
}

}



import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

/**
 * Created by 01139939 on 2017/5/20.
 */
public class FsHelper{
        public static void main(String[] args)  throws IOException {
           
            getFileList("/test/");
        }

        public static boolean isExists(String path) throws IOException{
            FileSystem fs = getFileSystem() ;
            return fs.exists(new Path(path));
        }
        public static long getLastModifyTime(String path) throws IOException{
            FileSystem fs = getFileSystem() ;
            FileStatus file = fs.getFileStatus(new Path(path));
            return file.getModificationTime();
        }
        public static boolean deleteFile(String path) throws IOException{
            FileSystem fs = getFileSystem() ;
            return fs.delete(new Path(path),false);//false 为是否递归删除
        }

        public static void rename( String fromFile,String toFile) throws IOException{
            FileSystem fs = getFileSystem() ;
            fs.rename(new Path(fromFile), new Path(toFile));

        }

        public static void createFile(String file)  throws IOException{
            FileSystem fs = getFileSystem() ;
            FSDataOutputStream fsd = fs.create(new Path(file));
            byte[] witeByte = "Hello world , you know".getBytes();
            fsd.write(witeByte, 0, witeByte.length);

            fsd.close();
            fs.close();
        }

        public static void putFile(String srcPath ,String dstPath) throws IOException{
            FileSystem fs = getFileSystem() ;

            Path src = new Path( srcPath);
            Path dst = new Path(dstPath);

            fs.copyFromLocalFile(src, dst);

            FileStatus[] fileStatus = fs.listStatus(dst);

            for(FileStatus status : fileStatus){
                System.out.println(status.getPath());
            }

            fs.close();
        }

        public static void mkdir(String dst) throws IOException{
            if(dst != null && !"".equals(dst)){
                FileSystem fs = getFileSystem();
                fs.mkdirs(new Path(dst));
                fs.close();
            }

        }

        public static FileSystem getFileSystem()  {
           /* Configuration cfg = new Configuration();
            cfg.addResource(new Path("D:\\code\\gitproject\\hadooprename\\src\\main\\resources\\core-site.xml"));
            cfg.addResource(new Path("D:\\code\\gitproject\\hadooprename\\src\\main\\resources\\hdfs-site.xml"));
            FileSystem fs = FileSystem.get(cfg);*/
            FileSystem fs= null;
            try {
                fs = FileSystem.get(new URI("/"), new Configuration());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            return fs;
        }

    /**
     * 读取path下的所有文件
     * @param path
     * @return
     * @throws IOException
     */
    public static String[] getFileList(String path) throws IOException{

        //Configuration conf = new Configuration();
        FileSystem fs =getFileSystem();
        List<String> files = new ArrayList<String>();
        Path s_path = new Path(path);
        if(fs.exists(s_path)){

            for(FileStatus status:fs.listStatus(s_path)){

                String pathName=status.getPath().getName();
                System.out.println(pathName);
                String orignalPath=status.getPath().toString();
                if(pathName.startsWith(".")){
                    System.out.println(orignalPath.substring(0,orignalPath.lastIndexOf("/")));
                    rename(orignalPath,orignalPath.substring(0,orignalPath.lastIndexOf("/"))+"/--"+pathName);
                }
                files.add(status.getPath().toString());
                System.out.println(status.getPath().toString());

            }
        }
        fs.close();

        return files.toArray(new String[]{});
    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值