学习apache commons-io 1.4类库中的FileCleaner(文件清除器)
它被用来清理象上传大文件时产生的临时文件之类,这些临时文件在不再被使用的时候会自动被删除.这会被
org.apache.commons.io.FileCleaningTracker的一个实例启动的一个守护线程默默执行.
共有三个类:
1、 类FileDeleteStrategy
import java.io.File;
import java.io.IOException;
public class FileDeleteStrategy {
public static final FileDeleteStrategy NORMAL = new FileDeleteStrategy("Normal");
public static final FileDeleteStrategy FORCE = new ForceFileDeleteStrategy();
private final String name;
protected FileDeleteStrategy(String name) {
this.name = name;
}
public boolean deleteQuietly(File fileToDelete) {
if (fileToDelete == null || fileToDelete.exists() == false) {
System.out.println("File delled!!");
return true;
}
try {
return doDelete(fileToDelete);
} catch (IOException ex) {
return false;
}
}
public void delete(File fileToDelete) throws IOException {
if (fileToDelete.exists() && doDelete(fileToDelete) == false) {
throw new IOException("Deletion failed: " + fileToDelete);
}
}
protected boolean doDelete(File fileToDelete) throws IOException {
return fileToDelete.delete();
}
public String toString() {
return "FileDeleteStrategy[" + name + "]";
}
static class ForceFileDeleteStrategy extends FileDeleteStrategy {
ForceFileDeleteStrategy() {
super("Force");
}
protected boolean doDelete(File fileToDelete) throws IOException {
// FileUtils.forceDelete(fileToDelete);
return true;
}
}
}
2、FileCleaningTracker类,这个类用于跟踪要删除的文件
import java.io.File;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.Collection;
import java.util.Vector;
public class FileCleaningTracker {
ReferenceQueue q = new ReferenceQueue();//引用队列
final Collection trackers = new Vector(); // synchronized
/**
* Whether to terminate the thread when the tracking is complete.
*/
volatile boolean exitWhenFinished = false;
//用于删除文件的线程
Thread reaper;
public void track(File file, Object marker) {
track(file, marker, (FileDeleteStrategy) null);
}
public void track(File file, Object marker, FileDeleteStrategy deleteStrategy) {
if (file == null) {
throw new NullPointerException("The file must not be null");
}
addTracker(file.getPath(), marker, deleteStrategy);
}
public void track(String path, Object marker) {
track(path, marker, (FileDeleteStrategy) null);
}
public void track(String path, Object marker, FileDeleteStrategy deleteStrategy) {
if (path == null) {
throw new NullPointerException("The path must not be null");
}
addTracker(path, marker, deleteStrategy);
}
private synchronized void addTracker(String path, Object marker, FileDeleteStrategy deleteStrategy) {
// synchronized block protects reaper
if (exitWhenFinished) {
throw new IllegalStateException("No new trackers can be added once exitWhenFinished() is called");
}
if (reaper == null) {
reaper = new Reaper();
reaper.start();
}
trackers.add(new Tracker(path, deleteStrategy, marker, q));
}
public int getTrackCount() {
return trackers.size();
}
public synchronized void exitWhenFinished() {
// synchronized block protects reaper
exitWhenFinished = true;
if (reaper != null) {
synchronized (reaper) {
reaper.interrupt();
}
}
}
private final class Reaper extends Thread {
Reaper() {
super("File Reaper");
setPriority(Thread.MAX_PRIORITY);
setDaemon(true);//设置为守护线程
}
public void run() {
// thread exits when exitWhenFinished is true and there are no more tracked objects
while (exitWhenFinished == false || trackers.size() > 0) {
Tracker tracker = null;
try {
// Wait for a tracker to remove.
tracker = (Tracker) q.remove();
} catch (Exception e) {
continue;
}
if (tracker != null) {
tracker.delete();
tracker.clear();
trackers.remove(tracker);
}
}
}
}
private static final class Tracker extends PhantomReference {//虚引用
private final String path;
private final FileDeleteStrategy deleteStrategy;
Tracker(String path, FileDeleteStrategy deleteStrategy, Object marker, ReferenceQueue queue) {
super(marker, queue);
this.path = path;
this.deleteStrategy = (deleteStrategy == null ? FileDeleteStrategy.NORMAL : deleteStrategy);
}
public boolean delete() {
return deleteStrategy.deleteQuietly(new File(path));
}
}
}
3、FileCleaner类
import java.io.File;
public class FileCleaner {
/**
* The instance to use for the deprecated, static methods.
*/
static final FileCleaningTracker theInstance = new FileCleaningTracker();
public static void track(File file, Object marker) {
theInstance.track(file, marker);
}
public static void track(File file, Object marker, FileDeleteStrategy deleteStrategy) {
theInstance.track(file, marker, deleteStrategy);
}
public static void track(String path, Object marker) {
theInstance.track(path, marker);
}
public static void track(String path, Object marker, FileDeleteStrategy deleteStrategy) {
theInstance.track(path, marker, deleteStrategy);
}
public static int getTrackCount() {
return theInstance.getTrackCount();
}
public static synchronized void exitWhenFinished() {
theInstance.exitWhenFinished();
}
public static FileCleaningTracker getInstance() {
return theInstance;
}
//简单测试
public static void main(String[] args){
getInstance().track("c:\\java\\1.txt", new Object());
getInstance().track("c:\\java\\2.txt",new Object());
System.gc();
System.exit(0);
}
}