Path 对路径进行解析,将参数转换为标准的URI格式,对Path的参数作判断,标准化,字符化等操作。为了便于理解Path,
各位可以先参看URI的详解,链接http://www.cnblogs.com/springside5/archive/2012/05/06/2486245.html
import java.net.*;
import java.io.*;
import org.apache.hadoop.conf.Configuration;
/** Names a file or directory in a {@link FileSystem}.
* Path strings use slash as the directory separator. A path string is
* absolute if it begins with a slash.
*/
public class Path implements Comparable {
/** The directory separator, a slash. */
public static final String SEPARATOR = "/";
public static final char SEPARATOR_CHAR = '/';
public static final String CUR_DIR = ".";
static final boolean WINDOWS
= System.getProperty("os.name").startsWith("Windows");
private URI uri; // a hierarchical uri //定义了Path类中的私有变量uri,路径以uri的格式表示,其后对Path的操作也主要通过对uri的操作实现。
/** Resolve a child path against a parent path. */
public Path(String parent, String child) { // 构造函数,我们可以发现其最终转换为Path(Path,Path)的构造函数。
this(new Path(parent), new Path(child));
}
/** Resolve a child path against a parent path. */
public Path(Path parent, String child) {
this(parent, new Path(child));
}
/** Resolve a child path against a parent path. */
public Path(String parent, Path child) {
this(new Path(parent), child);
}
/** Resolve a child path against a parent path. */
public Path(Path parent, Path child) { //对parent和child做规范化处理
// Add a slash to parent's path so resolution is compatible with URI's
URI parentUri = parent.uri;
String parentPath = parentUri.getPath();
if (!(parentPath.equals("/") || parentPath.equals(""))) //重新构造parentUri,在路径的结尾添加一个“/”,让parentUri与URI兼容
try {
parentUri = new URI(parentUri.getScheme(), parentUri.getAuthority(),
parentUri.getPath()+"/", null, parentUri.getFragment());
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
URI resolved = parentUri.resolve(child.uri);
initialize(resolved.getScheme(), resolved.getAuthority(),
normalizePath(resolved.getPath()),