日期:2014-05-17  浏览次数:20681 次

学习apache commons-io类库中的文件清除器
学习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);//设置为守护线程