import java.io.ByteArrayOutputStream;java
import java.io.FileInputStream;apache
import java.io.FileOutputStream;數組
import java.io.InputStream;ide
import java.net.URL;工具
import org.apache.hadoop.conf.Configuration;oop
import org.apache.hadoop.fs.FSDataInputStream;spa
import org.apache.hadoop.fs.FSDataOutputStream;.net
import org.apache.hadoop.fs.FileStatus;xml
import org.apache.hadoop.fs.FileSystem;對象
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
public class fileUtil {
/**
* 列出文件目錄結構
*/
@Test
public void listNum() throws Exception {
//建立configuration對象,有個默認的加載順序,先從core-default.xml,再到src目錄中的文件,這裏
//咱們給定了
Configuration conf = new Configuration();
//修改當前文件的副本數量
conf.set("dfs.replication", "4");
//修改當前文件的塊大小
conf.set("dfs.blocksize", "20480");
FileSystem fs = FileSystem.get(conf);
//定義一個URL的字符串
String file = "hdfs://hadoop01:9000/user/hadoop/data";
//經過一個URL的字符串構建一個path對象
Path path = new Path(file);
FileStatus[] fst = fs.listStatus(path);
for(FileStatus fst0 : fst) {
System.out.println(fst0.getPath() + ": isFile() =" + fst0.isFile());
}
//工具類,直接將FileStatus數組提取數據,造成Path[]
System.out.println("-------------------------------");
Path[] paths =FileUtil.stat2Paths(fst);
for (Path p : paths){
System.out.println(p.toString());
}
}
}