Java 日看一类(13)之IO包中的DefaultFileSystem类和DeleteOnExitHook类
2018-02-24 14:14
218 查看
这两个类都是非public类,也就是保留类,仅能被同一个包中的代码所使用
DefaultFileSystem类没有任何的继承关系,也未引入任何包(甚至连类头注释都没有)
代码如下(就一个方法,不存在变量
public static FileSystem getFileSystem() {
return new UnixFileSystem();
}作用是返回默认的Unix文件系统对象(默认构造方法给的是默认对象)
DeleteOnExitHook类引入了以下包
import java.util.*;
import java.io.File;不存在继承关系
类头注释如下:/**
* This class holds a set of filenames to be deleted on VM exit through a shutdown hook.
* A set is used both to prevent double-insertion of the same file as well as offer
* quick removal.
*/大意为:
这个类记录了在虚拟机关闭时将被删除的文件名,通过关闭触发(删除)
被用于防止文件的重复插入和提供快速删除功能
该类含有一个成员变量:
记录文件名的具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现
含有三个方法:
静态构造方法(默认private DeleteOnExitHook() {}向链接列表中添加文件static synchronized void add(String file) {
if(files == null) {
// DeleteOnExitHook is running. Too late to add a file
throw new IllegalStateException("Shutdown in progress");
}
files.add(file);
}进行删除工作static void runHooks() {
LinkedHashSet<String> theFiles;
synchronized (DeleteOnExitHook.class) {//数据绑定
theFiles = files;
files = null;//清空该类的记录
}
ArrayList<String> toBeDeleted = new ArrayList<>(theFiles);
// reverse the list to maintain previous jdk deletion order.
// Last in first deleted.
Collections.reverse(toBeDeleted);//反转排列顺序(反转后类似栈,后添加先删除)
for (String filename : toBeDeleted) {
(new File(filename)).delete();
}
}
含有一个静态代码块:检测该类的执行触发条件static {
// DeleteOnExitHook must be the last shutdown hook to be invoked.
// Application shutdown hooks may add the first file to the
// delete on exit list and cause the DeleteOnExitHook to be
// registered during shutdown in progress. So set the
// registerShutdownInProgress parameter to true.
sun.misc.SharedSecrets.getJavaLangAccess()
.registerShutdownHook(2 /* Shutdown hook invocation order */,
true /* register even if shutdown in progress */,
new Runnable() {
public void run() {
runHooks();
}
}
);
}
这两个类都比较简单,同时也是保留类,大概看下即可
DefaultFileSystem类没有任何的继承关系,也未引入任何包(甚至连类头注释都没有)
代码如下(就一个方法,不存在变量
public static FileSystem getFileSystem() {
return new UnixFileSystem();
}作用是返回默认的Unix文件系统对象(默认构造方法给的是默认对象)
DeleteOnExitHook类引入了以下包
import java.util.*;
import java.io.File;不存在继承关系
类头注释如下:/**
* This class holds a set of filenames to be deleted on VM exit through a shutdown hook.
* A set is used both to prevent double-insertion of the same file as well as offer
* quick removal.
*/大意为:
这个类记录了在虚拟机关闭时将被删除的文件名,通过关闭触发(删除)
被用于防止文件的重复插入和提供快速删除功能
该类含有一个成员变量:
记录文件名的具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现
private static LinkedHashSet<String> files = new LinkedHashSet<>();
含有三个方法:
静态构造方法(默认private DeleteOnExitHook() {}向链接列表中添加文件static synchronized void add(String file) {
if(files == null) {
// DeleteOnExitHook is running. Too late to add a file
throw new IllegalStateException("Shutdown in progress");
}
files.add(file);
}进行删除工作static void runHooks() {
LinkedHashSet<String> theFiles;
synchronized (DeleteOnExitHook.class) {//数据绑定
theFiles = files;
files = null;//清空该类的记录
}
ArrayList<String> toBeDeleted = new ArrayList<>(theFiles);
// reverse the list to maintain previous jdk deletion order.
// Last in first deleted.
Collections.reverse(toBeDeleted);//反转排列顺序(反转后类似栈,后添加先删除)
for (String filename : toBeDeleted) {
(new File(filename)).delete();
}
}
含有一个静态代码块:检测该类的执行触发条件static {
// DeleteOnExitHook must be the last shutdown hook to be invoked.
// Application shutdown hooks may add the first file to the
// delete on exit list and cause the DeleteOnExitHook to be
// registered during shutdown in progress. So set the
// registerShutdownInProgress parameter to true.
sun.misc.SharedSecrets.getJavaLangAccess()
.registerShutdownHook(2 /* Shutdown hook invocation order */,
true /* register even if shutdown in progress */,
new Runnable() {
public void run() {
runHooks();
}
}
);
}
这两个类都比较简单,同时也是保留类,大概看下即可
相关文章推荐
- Java 日看一类(9)之IO包中的CharConversionException类和Console类
- Java 日看一类(29)之IO包中的InputStreamReader类
- Java 日看一类(25)之IO包中的FilterOutputStream类
- Java 日看一类(34)之IO包中的NotActiveException和NotSerializableException
- Java 日看一类(5)之IO包中的BufferedWriter类
- Java 日看一类 (33)之IO包中的LineNumberReader类
- Java 日看一类(18)之IO包中的FileNameFilter接口类和FileNotFoundException异常类
- Java 日看一类(35)之IO包中的ObjectInput接口
- Java 日看一类(11)之IO包中的DataInputStream类
- Java 日看一类(32)之IO包中的IOException类
- Java 日看一类(4)之IO包中的BufferedReader类
- Java 日看一类(23)之IO包中的FileWriter类
- Java 日看一类(14)之IO包中的EOFException异常类和ExpiringCache内部类
- Java 日看一类(17)之IO包中的FileInputStream类
- Java 日看一类(20)之IO包中的FilePermission类与FilePermissionCollection类
- Java 日看一类(22)之IO包中的FileSystem类(抽象类
- Java 日看一类(3)之IO包中的BufferedOutputStream类
- Java 日看一类(28)之IO包中的Flushable接口和InputStream类
- Java 日看一类(16) 之IO包中的FileDescriptor类
- Java 日看一类(30)之IO包中的InterruptedIOException、InvalidObjectException异常类