FileSystem
实例提供了建立目录的方法正则表达式
public boolean mkdirs(Path f) throws IOException
apache
这个方法一次性建立全部必要但尚未的父目录编程
一般不须要显式建立一个目录,由于调用create()
方法写入文件时会自动建立全部父目录数组
FileStatus
类封装了文件系统中文件和目录的元数据包括文件长度,块大小,副本,修改时间,全部者,权限信息FileSystem
的getFileStatus
方法用于获取文件或目录的FileStatus
对象exists()
方法检查文件或者目录是否存在使用FileSystem
的listStatus()
方法bash
public FileStatus[] listStatus(Path f) throws IOException public FileStatus[] listStatus(Path f, PathFilter filter) throws IOException public FileStatus[] listStatus(Path[] files) throws IOException public FileStatus[] listStatus(Path[] files, PathFilter filter) throws IOException
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FileUtil; import org.apache.hadoop.fs.Path; import java.io.IOException; import java.net.URI; public class ListStatus { public static void main(String[] args) throws IOException { String uri = args[0]; Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(uri), conf); Path[] paths = new Path[args.length]; for (int i=0; i < paths.length; ++i) { paths[i] = new Path(args[i]); } FileStatus[] status = fs.listStatus(paths); // stat2Path2方法将一个FileStatus对象数组转换为一个Path对象数组 Path[] listedPaths = FileUtil.stat2Paths(status); for (Path p : listedPaths) { System.out.println(p); } } }
javac ListStatus.java
ide
hadoop ListStatus hdfs://localhost:9000/user/hadoop/input hdfs://localhost:9000/user/hadoop/output
oop
Hadoop为执行通配1提供了两个FileSystem
方法this
public FileStatus[] globStatus(Path pathPattern) throws IOException public FileStatus[] globStatus(Path pathPattern, PathFilter filter) throws IOException
globStatus()
方法返回与其路径匹配于指定模式的全部文件的FileStatus
对象数组,并按路径排序PathFilter
命令做为可选项能够进一步对匹配结果进行限制Hadoop支持的通配符与Unix bash的相同.net
通配符 | 名称 | 匹配 |
---|---|---|
* | 星号 | 匹配0或多个字符 |
? | 问号 | 匹配单一字符 |
[ab] | 字符类 | 匹配{a,b}集合中的一个字符 |
[^ab] | 非字符类 | 匹配非{a,b}集合中的一个字符 |
[a-b] | 字符范围 | 匹配一个在a-b范围内的字符(包括a,b),a在字典顺序上要小于或等于b |
[^a-b] | 非字符范围 | 匹配一个不在a-b范围内的字符(包括a,b),a在字典顺序上要小于或等于b |
{a,b} | 或选择 | 匹配包含a或b中的一个的表达式 |
\c | 转义字符 | 匹配元字符c |
FileSystem
中的listStatus()
和 globStatus()
方法提供了可选的 PathFilter
对象, 以编程方式控制通配符package org.apache.hadoop.fs; public interface PathFilter { boolean accept(Path path); }
pathFilter
和 java.io.FileFilter
同样,是 Path
对象, 而不是 File
对象import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.PathFilter; public class RegexExcludePathFilter implements PathFilter { private final String regex; public RegexExcludePathFilter(String regex) { this.regex = regex; } @Override public boolean accept(Path path) { return !path.toString().matches(regex); } }
使用 FileSystem
的 delete()
方法能够永久性删除文件或目录
public boolean delete(Path f, boolean recursive) throws IOException
recursive
的值会被忽略recursive
值为 true
时,非空目录及其内容才会被删除, 不然会抛出IOException异常在一个表达式中使用通配符来匹配多个文件是比较方便的,无需列举每一个文件和目录来指定输入,该操做称为"通配"↩