List file status in hadoop FileSystem - Java Big Data

Java examples for Big Data:Hadoop

Description

List file status in hadoop FileSystem

Demo Code



import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;

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 Task2 {

  public static void main(String[] args) throws IOException {
    String uri =args[0];//w  w  w . j a  v  a2 s . c  om
    Path path = new Path(uri);
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), conf);
    displayrecursively(fs, path);

  }
  
  public static void displayrecursively(FileSystem fs,Path path) throws FileNotFoundException, IOException
  {
    FileStatus[] filestatus = fs.listStatus(path);
    
    for(FileStatus fstat:filestatus)
    {
      if(fstat.isDirectory())
        System.out.println("Directory : "+fstat.getPath());
      
      displayrecursively(fs, fstat.getPath());
    }

  }

}

Related Tutorials