//FileSystem能够创建、删除文件、文件夹
//Path仅仅能够作为FileSystem的入口
//真正查看文件名、所属用户、大小等信息用的
//fs.listFiles(p, true);只能列出文件夹下面的文件,列不出文件夹
package org.harvest.frank;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
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 TestPath2 {
static FileSystem fs;
public static void main(String[] args) throws IOException, URISyntaxException {
// TODO Auto-generated method stub
Configuration conf = new Configuration();
// conf.addResource("core-site.xml");
// String url = "hdfs://mrcj:9000/test/output/part-r-00000";
String url = "hdfs://mrcj:9000/test/";
fs = FileSystem.get(new URI(url), conf);
Path p = new Path(url);
FileStatus fstat = fs.getFileStatus(p);
System.out.println("文件名称:"+p.getName());//part-r-00000
System.out.println("系统一个块大小:"+fstat.getBlockSize());//134217728=128M
System.out.println("文件访问时间:"+fstat.getAccessTime());//1514452493609
System.out.println("文件整体目录:"+fstat.getPath());//文件目录:hdfs://mrcj:9000/test/output/part-r-00000
System.out.println("文件大小:"+fstat.getLen());//10777=文件大小
System.out.println("文件所属用户:"+fstat.getOwner());//hdfs
System.out.println("是否是目录:"+fstat.isDirectory());//false
System.out.println("是否是文件:"+fstat.isFile());//true
System.out.println("------recursive()-----");
new TestPath2().recursive(p);
}
public void recursive(Path p) throws IllegalArgumentException, IOException{//遍历目录
// fs.listFiles(p, true);
FileStatus[] fStatus = fs.listStatus(p);
if(fStatus.length>0){
for(int i=0;i
if(fStatus[i].isDirectory()){
System.out.println(fStatus[i].getPath());
recursive(fStatus[i].getPath());
}else{
System.out.println(fStatus[i].getPath());
}
}
}
}
}