@Test
public void listFiles() throws Exception {
// listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path(
"hdfs://hadoop0:9000/flow"), true);
while (files.hasNext()) {
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}
System.out.println("---------------------------------");
// listStatus 可以列出文件和文件夹的信息,但是不提供自带的递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("/"));
recuersionFile(listStatus);
}
public void recuersionFile(FileStatus[] listStatus) throws Exception {
if (listStatus != null) {
for (FileStatus status : listStatus) {
if (status.isDirectory()) {
FileStatus[] listStatus2 = fs.listStatus(new Path(status.getPath().toString()));
recuersionFile(listStatus2);
}else{
String name = status.getPath().getName();
// System.out.println(status.getPath());
System.out.println("fileName: "+name);
}
}
public void listFiles() throws Exception {
// listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path(
"hdfs://hadoop0:9000/flow"), true);
while (files.hasNext()) {
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String fileName = filePath.getName();
System.out.println(fileName);
}
System.out.println("---------------------------------");
// listStatus 可以列出文件和文件夹的信息,但是不提供自带的递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("/"));
recuersionFile(listStatus);
}
public void recuersionFile(FileStatus[] listStatus) throws Exception {
if (listStatus != null) {
for (FileStatus status : listStatus) {
if (status.isDirectory()) {
FileStatus[] listStatus2 = fs.listStatus(new Path(status.getPath().toString()));
recuersionFile(listStatus2);
}else{
String name = status.getPath().getName();
// System.out.println(status.getPath());
System.out.println("fileName: "+name);
}
}
}
import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; /** * Created by 01139939 on 2017/5/20. */ public class FsHelper{ public static void main(String[] args) throws IOException { getFileList("/test/"); } public static boolean isExists(String path) throws IOException{ FileSystem fs = getFileSystem() ; return fs.exists(new Path(path)); } public static long getLastModifyTime(String path) throws IOException{ FileSystem fs = getFileSystem() ; FileStatus file = fs.getFileStatus(new Path(path)); return file.getModificationTime(); } public static boolean deleteFile(String path) throws IOException{ FileSystem fs = getFileSystem() ; return fs.delete(new Path(path),false);//false 为是否递归删除 } public static void rename( String fromFile,String toFile) throws IOException{ FileSystem fs = getFileSystem() ; fs.rename(new Path(fromFile), new Path(toFile)); } public static void createFile(String file) throws IOException{ FileSystem fs = getFileSystem() ; FSDataOutputStream fsd = fs.create(new Path(file)); byte[] witeByte = "Hello world , you know".getBytes(); fsd.write(witeByte, 0, witeByte.length); fsd.close(); fs.close(); } public static void putFile(String srcPath ,String dstPath) throws IOException{ FileSystem fs = getFileSystem() ; Path src = new Path( srcPath); Path dst = new Path(dstPath); fs.copyFromLocalFile(src, dst); FileStatus[] fileStatus = fs.listStatus(dst); for(FileStatus status : fileStatus){ System.out.println(status.getPath()); } fs.close(); } public static void mkdir(String dst) throws IOException{ if(dst != null && !"".equals(dst)){ FileSystem fs = getFileSystem(); fs.mkdirs(new Path(dst)); fs.close(); } } public static FileSystem getFileSystem() { /* Configuration cfg = new Configuration(); cfg.addResource(new Path("D:\\code\\gitproject\\hadooprename\\src\\main\\resources\\core-site.xml")); cfg.addResource(new Path("D:\\code\\gitproject\\hadooprename\\src\\main\\resources\\hdfs-site.xml")); FileSystem fs = FileSystem.get(cfg);*/ FileSystem fs= null; try { fs = FileSystem.get(new URI("/"), new Configuration()); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } return fs; } /** * 读取path下的所有文件 * @param path * @return * @throws IOException */ public static String[] getFileList(String path) throws IOException{ //Configuration conf = new Configuration(); FileSystem fs =getFileSystem(); List<String> files = new ArrayList<String>(); Path s_path = new Path(path); if(fs.exists(s_path)){ for(FileStatus status:fs.listStatus(s_path)){ String pathName=status.getPath().getName(); System.out.println(pathName); String orignalPath=status.getPath().toString(); if(pathName.startsWith(".")){ System.out.println(orignalPath.substring(0,orignalPath.lastIndexOf("/"))); rename(orignalPath,orignalPath.substring(0,orignalPath.lastIndexOf("/"))+"/--"+pathName); } files.add(status.getPath().toString()); System.out.println(status.getPath().toString()); } } fs.close(); return files.toArray(new String[]{}); } }