该类是个抽象类(也不能被包外调用
无继承和接口使用
引入了
import java.lang.annotation.Native;
该类的类头注释如下
/** * Package-private abstract class for the local filesystem abstraction. */
大意如下:
用于本地文件系统抽象化的私有包中的抽象类
该类含有如下的成员方法:
获得本地文件系统的名称分隔符
public abstract char getSeparator();
获得本地文件系统的路径分隔符
public abstract char getPathSeparator();
返回输入路径的标准化路径
public abstract String normalize(String path);
返回输入路径的前缀长度(输入路径必须是标准路径
public abstract int prefixLength(String path);
子路径绝对化(化为绝对路径,输入必须标准
public abstract String resolve(String parent, String child);
返回父路径(在File 类中父路径为空
public abstract String getDefaultParent();
返回路径的URI(会删去路径中的一些分隔符,File中的方法会处理这个类的返回值
public abstract String fromURIPath(String path);
判断该抽象路径是否为绝对路径
public abstract boolean isAbsolute(File f);
把给定的文件路径绝对化(通过调用getAbsolutePath(),和getCanonicalPath()
public abstract String resolve(File f);
路径规范化(不是很懂这个和上面的normalize()区别在哪
public abstract String canonicalize(String path) throws IOException;
返回传入文件或目录的属性(返回0为文件不存在,或者IO异常
public abstract int getBooleanAttributes(File f);
@Native public static final int BA_EXISTS = 0x01; @Native public static final int BA_REGULAR = 0x02; @Native public static final int BA_DIRECTORY = 0x04; @Native public static final int BA_HIDDEN = 0x08;
检测传入文件是否支持这些操作
public abstract boolean checkAccess(File f, int access);
@Native public static final int ACCESS_READ = 0x04; @Native public static final int ACCESS_WRITE = 0x02; @Native public static final int ACCESS_EXECUTE = 0x01;
为文件设置权限(包括权限名,设置还是取消,是否仅文件所有者)
public abstract boolean setPermission(File f, int access, boolean enable, boolean owneronly);
获得文件上次修改时间(为0则文件不存在或者异常
public abstract long getLastModifiedTime(File f);
获得文件长度(byte计数,为0则传入为目录或者不存在或异常
public abstract long getLength(File f);
创建给定路径的空文件(返回false时是文件已存在
public abstract boolean createFileExclusively(String pathname) throws IOException;
删除给定文件或目录(仅当删除成功时为true
public abstract boolean delete(File f);
返回目录下的所有元素(字符串数组
public abstract String[] list(File f);
创建给定路径的目录(仅当创建成功时为true
public abstract boolean createDirectory(File f);
重命名文件(将File1的路径名改为File2)
public abstract boolean rename(File f1, File f2);
设置最后修改时间(设置成功返回true
public abstract boolean setLastModifiedTime(File f, long time);
设置文件为只读(成功为true
public abstract boolean setReadOnly(File f);
罗列可用的文件系统根目录
public abstract File[] listRoots();
为文件获得磁盘空间
@Native public static final int SPACE_TOTAL = 0; @Native public static final int SPACE_FREE = 1; @Native public static final int SPACE_USABLE = 2; public abstract long getSpace(File f, int t);
字典序比较路径
public abstract int compare(File f1, File f2);
获得文件的hash值
public abstract int hashCode(File f);
使用系统的标准前缀和标准缓冲(没查到传入的值对应的环境参数,根据变量名猜测一下,应该是如此
static boolean useCanonCaches = true; static boolean useCanonPrefixCache = true; private static boolean getBooleanProperty(String prop, boolean defaultVal) { String val = System.getProperty(prop); if (val == null) return defaultVal; if (val.equalsIgnoreCase("true")) { return true; } else { return false; } } static { useCanonCaches = getBooleanProperty("sun.io.useCanonCaches", useCanonCaches); useCanonPrefixCache = getBooleanProperty("sun.io.useCanonPrefixCache", useCanonPrefixCache); }
该类大致分为几个部分(这些不是我分的,该类作者分的):
标准化和构建部分
路径操作部分
属性访问操作部分
文件操作部分
文件系统接口
基础设施构建
这些是按顺序对应上面我所写的方法,如果你能成功的把方法划分到这几部分中,恭喜你,你对文件系统的工作原理有了一个清晰的了解。