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

Java 如何获取目录中文件的绝对路径?,java,hadoop,bigdata,Java,Hadoop,Bigdata,我有一个包含文件、目录、子目录等的目录。如何使用Apache Hadoop API获取所有文件和目录的绝对路径列表?编写一个递归函数,该函数获取文件并检查其是否为目录,如果目录列出其中的所有文件,并在for循环中检查该文件是否为目录,然后递归调用或仅返回文件列表 下面类似的内容,但不完全相同(这里我只返回.java文件) private static List recursiveDir(文件){ 如果(!file.isDirectory()){ //System.out.println(“[”+

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

编写一个递归函数,该函数获取文件并检查其是否为目录,如果目录列出其中的所有文件,并在for循环中检查该文件是否为目录,然后递归调用或仅返回文件列表

下面类似的内容,但不完全相同(这里我只返回.java文件)

private static List recursiveDir(文件){
如果(!file.isDirectory()){
//System.out.println(“[”+file.getName()+“]不是有效的目录”);
返回null;
}
List returnList=new ArrayList();
File[]files=File.listFiles();
用于(文件f:文件){
如果(!f.isDirectory()){
if(f.getName().endsWith(“java”)){
返回名单。添加(f);
}
}否则{
addAll(recursiveDir(f));
}
}
退货清单;
}

对于hdfs,您可以使用hadoop fs-lsr。

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

谢谢,但我正在搜索如何使用ApacheHadoop实现这一点。但现在我知道了解决方案。@Peter Shipilo:一个小的更正,别忘了关闭文件系统实例。除其他外,您还需要hadoop hdfs的正确依赖项。或者
hdfs dfs ls-R
——但它没有给我们绝对路径,不是吗?问题是关于API,而不是CLI客户端。
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();
        }
    }
}