如何获得目录中文件的绝对路径?

2sbarzqh  于 2021-06-03  发布在  Hadoop
关注(0)|答案(3)|浏览(410)

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

5vf7fwbs

5vf7fwbs1#

使用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();
        }
    }
}
ghhkc1vu

ghhkc1vu2#

编写一个递归函数,它接收一个文件并检查它是否是目录,如果目录列出其中的所有文件,并在for循环中检查该文件是否是目录,然后递归调用或返回文件列表。
类似于下面的内容,但不完全相同(这里我只返回.java文件)

private static List<File> recursiveDir(File file) {
    if (!file.isDirectory()) {
//          System.out.println("[" + file.getName() + "] is not a valid directory");
        return null;
    }

    List<File> returnList = new ArrayList<File>();
    File[] files = file.listFiles();
    for (File f : files) {
        if (!f.isDirectory()) {
            if (f.getName().endsWith("java")) {
                returnList.add(f);
            }
        } else {
            returnList.addAll(recursiveDir(f));
        }
    }
    return returnList;
}
8yparm6h

8yparm6h3#

有了hdfs,就可以使用hadoopfs-lsr。

相关问题