File 类源代码解析

[size=medium]1. 分隔符[/size]


// 名称分隔符,Windows系统为反斜杠'\\',Linux系统为斜杠'/'
public static final char separatorChar = fs.getSeparator();

// 名称分隔字符串
public static final String separator = "" + separatorChar;

// 路径分隔符,Windows系统为分号';',Linux系统为冒号':'
public static final char pathSeparatorChar = fs.getPathSeparator();

// 路径分隔字符串
public static final String pathSeparator = "" + pathSeparatorChar;


[size=medium]2. 文件路径[/size]

// 标准的符号完全解析的绝对路径,具有唯一性,去除了绝对路径中可能存在的..或.,符号链接(Linux),标准化盘符字母大小写。
public String getCanonicalPath() throws IOException {
return fs.canonicalize(fs.resolve(this));
}

// 文件的绝对路径
public String getAbsolutePath() {
return fs.resolve(this);
}

// ile构造方法里的路径,可以是相对或绝对路径
public String getPath() {
return path;
}

// 父路径
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); // 返回分隔符之前的字符串
}

// 文件名称
public String getName() {
int index = path.lastIndexOf(separatorChar);
if (index < prefixLength) return path.substring(prefixLength);
return path.substring(index + 1);
}


测试:

System.out.println(System.getProperty("user.dir"));

System.out.println("-----Relative path: different return paths-----");
File file1 = new File("..\\src\\test1.txt");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getCanonicalPath());

System.out.println("-----Relative path: different return paths-----");
File file2 = new File(".\\test1.txt");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getCanonicalPath());

System.out.println("-----Absolute path: the same return paths-----");
File file3 = new File("D:\\workspace\\perfume\\src\\test1.txt");
System.out.println(file3.getPath());
System.out.println(file3.getAbsolutePath());
System.out.println(file3.getCanonicalPath());


输出:
D:\workspace\perfume
-----Relative path: different return paths-----
..\src\test1.txt
D:\workspace\perfume\..\src\test1.txt
D:\workspace\src\test1.txt
-----Relative path: different return paths-----
.\test1.txt
D:\workspace\perfume\.\test1.txt
D:\workspace\perfume\test1.txt
-----Absolute path: the same return paths-----
D:\workspace\perfume\src\test1.txt
D:\workspace\perfume\src\test1.txt
D:\workspace\perfume\src\test1.txt

[size=medium]3. 文件属性[/size]

// 是否可读
public boolean canRead() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return fs.checkAccess(this, FileSystem.ACCESS_READ);
}

// 是否可写
public boolean canWrite() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
return fs.checkAccess(this, FileSystem.ACCESS_WRITE);
}

// 文件是否存在
public boolean exists() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
}

// 是否是目录
public boolean isDirectory() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
!= 0);
}

// 是否是文件
public boolean isFile() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0);
}

// 是否隐藏
public boolean isHidden() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);
}

// 最近访问时间
public long lastModified() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return fs.getLastModifiedTime(this);
}

// 文件长度,如果是目录,返回值未指定
public long length() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return fs.getLength(this);
}


[size=medium]4. 文件操作[/size]

// 创建文件
public boolean createNewFile() throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) security.checkWrite(path);
return fs.createFileExclusively(path);
}

// 删除文件
public boolean delete() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkDelete(path);
}
return fs.delete(this);
}

// 在虚拟机正常终止的时候删除文件,删除文件的顺序和注册的时候相反,已经注册过的再次调用此方法没有影响
public void deleteOnExit() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkDelete(path);
}
DeleteOnExitHook.add(path);
}

// 获取当前路径所代表的目录下的文件和目录列表,如果不是目录,返回null。
// 返回的字符串是文件名,不是完整路径。
// 列表根据alphabetical来排序。
public String[] list() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return fs.list(this);
}

// 同上,加了个文件名称过滤
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()]);
}

// 同上,将路径名转换为文件
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;
}

// 创建目录,如果父目录不存在,抛异常
public boolean mkdir() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
return fs.createDirectory(this);
}

// 创建目录,如果父目录不存在,先创建父目录
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()); // 当且仅当父目录不为null,父目录创建成功或存在,当前目录创建成功时返回true
}

// 重命名文件
public boolean renameTo(File dest) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
security.checkWrite(dest.path);
}
return fs.rename(this, dest);
}


设置属性


// 设置文件为只读,直到设置为允许写或删除
public boolean setReadOnly() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
}
return fs.setReadOnly(this);
}


某些设置可能不起作用,如在Windows平台上,setReadable(false),文件的canRead()还是true:

String fileName = "D:/spark.txt";
File file = new File(fileName);
System.out.println("canRead: " + file.canRead());
System.out.println("canWrite: " + file.canWrite());
System.out.println("canExecute: " + file.canExecute());
System.out.println("-----After changing-----");
file.setReadable(false); // It doesn't take effect on Windows platform
System.out.println("canRead: " + file.canRead());
file.setWritable(false);
System.out.println("canWrite: " + file.canWrite());
file.setExecutable(false); // It doesn't take effect on Windows platform
System.out.println("canExecute: " + file.canExecute());


输出:
canRead: true
canWrite: true
canExecute: true
-----After changing-----
canRead: true
canWrite: false
canExecute: true
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值