java - 如何获取目录中文件的绝对路径?

标签 java hadoop bigdata

我有一个包含文件、目录、子目录等的目录。如何使用 Apache Hadoop API 获取所有文件和目录的绝对路径列表?

最佳答案

使用 HDFS API:

package org.myorg.hdfsdemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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



public class HdfsDemo {

    public static void main(String[] args) throws IOException {

        Configuration conf = new Configuration();
        conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/core-site.xml"));
        conf.addResource(new Path("/Users/miqbal1/hadoop-eco/hadoop-1.1.2/conf/hdfs-site.xml"));
        FileSystem fs = FileSystem.get(conf);
        System.out.println("Enter the directory name :");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Path path = new Path(br.readLine());
        displayDirectoryContents(fs, path);
    }

    private static void displayDirectoryContents(FileSystem fs, Path rootDir) {
        // TODO Auto-generated method stub
        try {

            FileStatus[] status = fs.listStatus(rootDir);
            for (FileStatus file : status) {
                if (file.isDir()) {
                    System.out.println("This is a directory:" + file.getPath());
                    displayDirectoryContents(fs, file.getPath());
                } else {
                    System.out.println("This is a file:" + file.getPath());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

关于java - 如何获取目录中文件的绝对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034758/

相关文章:

java - 当我的 DB 版本为 10.10 时,Netbeans 8.1 如何在 Derby DB 版本 10.5 上给出错误

hadoop - shuffle阶段和combiner阶段有什么区别?

hadoop - HBase:Regionserver和QuorumPeer之间的区别

java - 如何为此 Twitter API 与 Kafka-Producer 建立连接

Java mysql 插入jtable多条数据到mysql数据库

java - Mybatis 多对多最佳实践

java - 匿名 block 就像静态 block 一样

java - Hadoop的默认分组比较器?

python - 如何获取大型文本文件数据的统计信息

database - MonetDB 通过事件连接进行查询以后台最佳策略刷新数据