超详细的逐句介绍Java高级接口之File函数源码讲解(一)

一、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);
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝域时空

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值