11.14

1|11判断hdfs中文件是否存在

public static void existandcreat(String path) throws URISyntaxException, IOException, InterruptedException { FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); Path path1=new Path(path); if(fs.exists(path1)) { System.out.println("存在!!"); } else { FSDataOutputStream ops=fs.create(path1); ops.close(); fs.close(); } }
 

1|12递归查看目录下的文件信息

 

 
/*查看当前目录下的文件信息*/ public static void printfile(String file)throws URISyntaxException, IOException, InterruptedException { FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); FileStatus[] statuses=fs.listStatus(new Path(file)); for(FileStatus s:statuses) { System.out.println("读写权限:"+s.getPermission()+"; 文件大小:"+s.getBlockSize()+"; 文件路径:" +s.getPath()+"; 文件创建时间:"+s.getModificationTime()); } fs.close(); } /*递归查看目录下的文件信息*/ public static void prinfileInfo(String file)throws URISyntaxException, IOException, InterruptedException { FileSystem fs = FileSystem.get(new URI("hdfs://hadoop101:8020"), new Configuration(), "root"); Path path= new Path(file); RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(path, true); while(iterator.hasNext()) { FileStatus s = iterator.next(); System.out.println("读写权限:"+s.getPermission()+"; 文件大小:"+s.getBlockSize()+"; 文件路径:" +s.getPath()+"; 文件创建时间:"+s.getModificationTime()); } fs.close(); }
 

 

posted @ 2021-11-14 22:07  韩佳龙  阅读(76)  评论(0编辑  收藏  举报