java.io.File类是文件或路径的抽象表达,它实现了Serializable和Comparable接口,所以支持File对象的持久化,以及文件之间的大小比较。
1.static private FileSystem fs = FileSystem.getFileSystem();
分析:表示平台本地文件系统的文件系统对象。
2.private static enum PathStatus { INVALID, CHECKED };
分析:地址是否合法的enum类;
3.private transient PathStatus status = null;
分析:标记文件路径是否无效标志。transient表示瞬态的,序列化的时候不做持久化。
4.final boolean isInvalid()
final boolean isInvalid() {
if (status == null) {
status = (this.path.indexOf('\u0000') < 0) ? PathStatus.CHECKED
: PathStatus.INVALID;
}
return status == PathStatus.INVALID;
}
分析:检查文件是否有一个有效的路径。目前,对文件路径的检查非常有限。它只包含NUL字符的检查,返回true意味着路径是invalid/garbage,
5.private transient int prefixLength;
分析:抽象路径名的前缀的长度,如果没有前缀,则为0。使用transient修饰符表示持久化时忽略该属性。
6.int getPrefixLength()
int getPrefixLength() {
return prefixLength;
}
分析:返回抽象路径名的前缀的长度,用于文件系统类。
7.public static final char separatorChar = fs.getSeparator();
分析:与系统相关的默认名称分割符,此字段被初始化为包含系统属性第一个字符的文件分割符,在UNIX系统中此字段值是’\’,在WINDOWS系统中此字符的值是’\’。
8.public static final String separator = “” + separatorChar;
分析:与系统相关的默认名称分割符,转换为字符串类型。
9.public static final char pathSeparatorChar = fs.getPathSeparator();
分析:路径分割符,代表’:’字符类。
10.public static final String pathSeparator = “” + pathSeparatorChar;
分析:路径分割符,代表字符串类。
11.private File(String pathname, int prefixLength)
private File(String pathname, int prefixLength) {
this.path = pathname;
this.prefixLength = prefixLength;
}
分析:表示已经标准化的路径名称的内部构造函数。
12.private File(String child, File parent)
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;
}
分析:对已经标准化的路径名称的内部构造函数,参数顺序是用来从构造函数public(File, String)消除的方法。
13.public File(String pathname)
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
分析:创建一个File对象实例,将pathname字符串转换为抽象pathname,如果给定的字符串是空串,结果将是抽象空字符串。
14.public File(String parent, String child)
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对象实例通过父文件路径和子文件路径来实现。
15.public File(File parent, String child)
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文件对象,通过父文件对象和子文件路径来实现。
16.public File(URI uri)
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);
}
分析:通过URI实例对象创建一个File文件对象。
17.public String getName()
public String getName() {
int index = path.lastIndexOf(separatorChar);
if (index < prefixLength) return path.substring(prefixLength);
return path.substring(index + 1);
}
分析:返回文件名称或者对象对应的最有一个路径字符串。
18.public String getParent()
public String getParent() {
int index = path.lastIndexOf(separatorChar);
if (index < prefixLength) {
if ((prefixLength > 0) && (path.length() > prefixLength))
return path.substring(0, prefixLength);
return null;
}
return path.substring(0, index);
}
分析:返回当前File对象的上级路径名称。
19.public File getParentFile()
public File getParentFile() {
String p = this.getParent();
if (p == null) return null;
return new File(p, this.prefixLength);
}
分析:返回当前对象的上级目录的File对象。
20.public String getPath()
public String getPath() {
return path;
}
分析:将当前抽象路径名称转换为路径字符串。
21.public boolean isAbsolute()
public boolean isAbsolute() {
return fs.isAbsolute(this);
}
分析:判定当前File对象是否是绝对路径。
22.public String getAbsolutePath()
public String getAbsolutePath() {
return fs.resolve(this);
}
分析:获取当前对象的绝对路径。
23.public File getAbsoluteFile()
public File getAbsoluteFile() {
String absPath = getAbsolutePath();
return new File(absPath, fs.prefixLength(absPath));
}
分析:获取当前对象对应的绝对路径对应的File对象。
24.public String getCanonicalPath() throws IOException
public String getCanonicalPath() throws IOException {
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.canonicalize(fs.resolve(this));
}
分析:返回抽象路径名对应个规范路径字符串,规范路径名称既是规范的也是唯一的,规范形式的定义是系统依赖的,如果有必要该方法将会首先将路径名转换为绝对路径,然后将其转换为唯一的格式,
25.public File getCanonicalFile() throws IOException
public File getCanonicalFile() throws IOException {
String canonPath = getCanonicalPath();
return new File(canonPath, fs.prefixLength(canonPath));
}
分析:将当前文件对象转换为规范File对象。
26.public boolean canRead()
public boolean canRead() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
return fs.checkAccess(this, FileSystem.ACCESS_READ);
}
分析:判断应用程序是否可以读取此抽象路径名称表示的文件。
27.public boolean canWrite()
public boolean canWrite() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
return fs.checkAccess(this, FileSystem.ACCESS_WRITE);
}
分析:判断应用程序是否可以修改此抽象路径名称表示的文件。
28.public boolean exists()
public boolean exists() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
}
分析:判断抽象路径名称给定的文件或者路径是否存在。
29.public boolean isDirectory()
public boolean isDirectory() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
!= 0);
}
分析:判断给定File对象是否是一个目录对象。
30.public boolean isFile()
public boolean isFile() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0);
}
分析:判断File对象给定的路径名称是否是一个规范的文件。
31.public boolean isHidden()
public boolean isHidden() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return false;
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);
}
分析:判断抽象路径名指定的文件是否是一个隐藏的文件。
32.public long lastModified()
public long lastModified() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
return fs.getLastModifiedTime(this);
}
分析:返回抽象路径名称指定的文件的最后修改时间。
33.public long length()
public long length() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
return fs.getLength(this);
}
分析:返回给定的抽象路径名称的文件长度。
34.public boolean createNewFile() throws IOException
public boolean createNewFile() throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkWrite(path);
if (isInvalid()) {
throw new IOException("Invalid file path");
}
return fs.createFileExclusively(path);
}
分析:自动的创建一个新的空的文件,以给定的抽象路径名称为文件名。
35.public boolean delete()
public boolean delete() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkDelete(path);
}
if (isInvalid()) {
return false;
}
return fs.delete(this);
}
分析:删除抽象路径名称给定的文件或者目录。
36.public void deleteOnExit()
public void deleteOnExit() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkDelete(path);
}
if (isInvalid()) {
return;
}
DeleteOnExitHook.add(path);
}
分析:当虚拟机停止的时候删除抽象路径名称指定的文件或者目录。
37.public String[] list()
public String[] list() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
if (isInvalid()) {
return null;
}
return fs.list(this);
}
分析:返回此抽象路径名指定的文件或者目录的集合。
38.public String[] list(FilenameFilter filter)
public String[] list(FilenameFilter filter) {
String names[] = list();
if ((names == null) || (filter == null)) {
return names;
}
List<String> v = new ArrayList<>();
for (int i = 0 ; i < names.length ; i++) {
if (filter.accept(this, names[i])) {
v.add(names[i]);
}
}
return v.toArray(new String[v.size()]);
}
分析:返回此抽象路径名及指定的筛选器指定的文件或者目录的集合。
39.public File[] listFiles()
public File[] listFiles() {
String[] ss = list();
if (ss == null) return null;
int n = ss.length;
File[] fs = new File[n];
for (int i = 0; i < n; i++) {
fs[i] = new File(ss[i], this);
}
return fs;
}
分析:返回此抽象路径名指定的文件或者目录下的所有文件的集合。
40.public File[] listFiles(FilenameFilter filter)
public File[] listFiles(FilenameFilter filter) {
String ss[] = list();
if (ss == null) return null;
ArrayList<File> files = new ArrayList<>();
for (String s : ss)
if ((filter == null) || filter.accept(this, s))
files.add(new File(s, this));
return files.toArray(new File[files.size()]);
}
分析:返回此抽象路径名及过滤器指定的文件或者目录下的所有文件的集合。
41.public boolean mkdir()
public boolean mkdir() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
return fs.createDirectory(this);
}
分析:创建此抽象路径名指定的目录。
42.public boolean mkdirs()
public boolean mkdirs() {
if (exists()) {
return false;
}
if (mkdir()) {
return true;
}
File canonFile = null;
try {
canonFile = getCanonicalFile();
} catch (IOException e) {
return false;
}
File parent = canonFile.getParentFile();
return (parent != null && (parent.mkdirs() || parent.exists()) &&
canonFile.mkdir());
}
分析:创建抽象路径名指定的目录及任何的父目录。
43.public boolean renameTo(File dest)
public boolean renameTo(File dest) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
security.checkWrite(dest.path);
}
if (dest == null) {
throw new NullPointerException();
}
if (this.isInvalid() || dest.isInvalid()) {
return false;
}
return fs.rename(this, dest);
}
分析:重命名给定的抽象路径名。
44.public boolean setLastModified(long time)
public boolean setLastModified(long time) {
if (time < 0) throw new IllegalArgumentException("Negative time");
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
return fs.setLastModifiedTime(this, time);
}
分析:设置抽象路径名称指定的文件或者目录的最后修改时间。
45.public boolean setReadOnly()
public boolean setReadOnly() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
return fs.setReadOnly(this);
}
分析:设置给定的抽象路径名为只读模式。
46.public boolean setWritable(boolean writable, boolean ownerOnly)
public boolean setWritable(boolean writable, boolean ownerOnly) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
if (isInvalid()) {
return false;
}
return fs.setPermission(this, FileSystem.ACCESS_WRITE, writable, ownerOnly);
}
分析:设置给定的抽象路径名是当前所有者或者所有人都可以写。
47.public long getTotalSpace()
public long getTotalSpace() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
sm.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
return fs.getSpace(this, FileSystem.SPACE_TOTAL);
}
分析:返回抽象路径名指定的目录或文件的大小。
48.public long getFreeSpace()
public long getFreeSpace() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
sm.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
return fs.getSpace(this, FileSystem.SPACE_FREE);
}
分析:返回抽象路径名指定的目录或文件的空闲分区。
49. public long getUsableSpace()
public long getUsableSpace() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
sm.checkRead(path);
}
if (isInvalid()) {
return 0L;
}
return fs.getSpace(this, FileSystem.SPACE_USABLE);
}
分析:返回此抽象路径名指定的文件或者目录在虚拟机可用的字节数。