一、File类
File类是所有文件输入输出的父类。File 类是 java.io 包中唯一代表磁盘文件本身的对象,如果希望在程序中操作文件和目录,则都可以通过 File 类来完成。下面我将从源码角度详细介绍一File类的内部初始化方法。
二、内部初始化方法
下面定义了File类的类名,它继承了序列化接口和文件泛型
public class File
implements Serializable, Comparable<File>
{
}
下面定义了获取默认文件位置fs
private static final FileSystem fs = DefaultFileSystem.getFileSystem();
定义了文件路径path
private final String path;
利用枚举定义文件路径状态
private static enum PathStatus { INVALID, CHECKED };
定义一个转瞬即逝的文件路径状态
private transient PathStatus status = null;
判断文件状态是否有效
final boolean isInvalid() {
if (status == null) {
status = (this.path.indexOf('\u0000') < 0) ? PathStatus.CHECKED
: PathStatus.INVALID;
}
return status == PathStatus.INVALID;
}
获取前缀长度
int getPrefixLength() {
return prefixLength;
}
获取分隔符
public static final char separatorChar = fs.getSeparator();
定义分隔符
public static final String separator = "" + separatorChar;
获取路径分隔符
public static final char pathSeparatorChar = fs.getPathSeparator();
定义路径分隔符
public static final String pathSeparator = "" + pathSeparatorChar;
已经规范化的路径名字符串的内部构造函数
private File(String pathname, int prefixLength) {
this.path = pathname;
this.prefixLength = prefixLength;
}
已经规范化的路径名字符串的内部构造函数。参数顺序用于消除此方法与 public(File, String) 构造函数的歧义。
private File(String child, File parent) {
assert parent.path != null;
assert (!parent.path.equals(""));
this.path = fs.resolve(parent.path, child);
this.prefixLength = parent.prefixLength;
}
定义了File类的构造方法
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
从父路径名字符串和子路径名字符串创建一个新的 File 实例。
public File(String parent, String child) {
if (child == null) {
throw new NullPointerException();
}
if (parent != null) {
if (parent.equals("")) {
this.path = fs.resolve(fs.getDefaultParent(),
fs.normalize(child));
} else {
this.path = fs.resolve(fs.normalize(parent),
fs.normalize(child));
}
} else {
this.path = fs.normalize(child);
}
this.prefixLength = fs.prefixLength(this.path);
}
从父路径名字符串和子路径名字符串创建一个新的 File 实例。
public File(File parent, String child) {
if (child == null) {
throw new NullPointerException();
}
if (parent != null) {
if (parent.path.equals("")) {
this.path = fs.resolve(fs.getDefaultParent(),
fs.normalize(child));
} else {
this.path = fs.resolve(parent.path,
fs.normalize(child));
}
} else {
this.path = fs.normalize(child);
}
this.prefixLength = fs.prefixLength(this.path);
}
通过将给定的 file: URI 转换为抽象路径名来创建一个新的 File 实例。
public File(URI uri) {
// Check our many preconditions
if (!uri.isAbsolute())
throw new IllegalArgumentException("URI is not absolute");
if (uri.isOpaque())
throw new IllegalArgumentException("URI is not hierarchical");
String scheme = uri.getScheme();
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
throw new IllegalArgumentException("URI scheme is not \"file\"");
if (uri.getAuthority() != null)
throw new IllegalArgumentException("URI has an authority component");
if (uri.getFragment() != null)
throw new IllegalArgumentException("URI has a fragment component");
if (uri.getQuery() != null)
throw new IllegalArgumentException("URI has a query component");
String p = uri.getPath();
if (p.equals(""))
throw new IllegalArgumentException("URI path component is empty");
// Okay, now initialize
p = fs.fromURIPath(p);
if (File.separatorChar != '/')
p = p.replace('/', File.separatorChar);
this.path = fs.normalize(p);
this.prefixLength = fs.prefixLength(this.path);
}