`

学习apache commons-io类库中的文件清除器

    博客分类:
  • java
 
阅读更多
学习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);
    }
    
}
分享到:
评论

相关推荐

    commons-io-2.5-API文档-中文版.zip

    赠送Maven依赖信息文件:commons-io-2.5.pom; 包含翻译后的API文档:commons-io-2.5-javadoc-API文档-中文(简体)版.zip; Maven坐标:commons-io:commons-io:2.5; 标签:commons、io、中文文档、jar包、java; ...

    commons-io-2.8.0-API文档-中英对照版.zip

    赠送Maven依赖信息文件:commons-io-2.8.0.pom; 包含翻译后的API文档:commons-io-2.8.0-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:commons-io:commons-io:2.8.0; 标签:commons、中英对照文档、...

    commons-io-1.3.2-API文档-中文版.zip

    赠送Maven依赖信息文件:commons-io-1.3.2.pom; 包含翻译后的API文档:commons-io-1.3.2-javadoc-API文档-中文(简体)版.zip; Maven坐标:commons-io:commons-io:1.3.2; 标签:commons、io、中文文档、jar包、java...

    commons-io-2.11.0-API文档-中文版.zip

    赠送Maven依赖信息文件:commons-io-2.11.0.pom; 包含翻译后的API文档:commons-io-2.11.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:commons-io:commons-io:2.11.0; 标签:commons、中文文档、jar包、java...

    Apache commons-io-2.5.jar

    Apache Commons IO 2.5 (要求 JDK 1.6),IOUtils,FileUtils,jar包下载

    commons-io-2.7-API文档-中文版.zip

    赠送Maven依赖信息文件:commons-io-2.7.pom; 包含翻译后的API文档:commons-io-2.7-javadoc-API文档-中文(简体)版.zip; Maven坐标:commons-io:commons-io:2.7; 标签:commons、jar包、java、中文文档; 使用...

    开发工具 commons-io-1.3.2

    开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2开发工具 commons-io-1.3.2...

    commons-fileupload-1.3.3.jar和commons-io-2.6.jar

    commons-fileupload-1.3.3.jar和commons-io-2.6.jar最新版本

    java+servlet+commons-io-2.4.jar+commons-fileupload-1.3.jar实现文件的上传与下载

    java+servlet+commons-io-2.4.jar+commons-fileupload-1.3.jar实现文件的上传与下载

    commons-io-2.0.1大全

    Commons IO是apache的一个开源的工具包,封装了IO操作的相关类,包含了最新的commons-io-2.0.1-bin,commons-io-2.0.1-src,commons-io-2.0.1-doc

    commons-io-2.6.jar下载

    commons-io-2.6.jar下载

    commons-io-2.5.jar

    commons-io-2.5 <groupId>org.apache.commons <artifactId>commons-parent <version>39 <modelVersion>4.0.0 <groupId>commons-io <artifactId>commons-io <version>2.5 <name>Apache Commons IO</name>

    commons-io-2.11.0-API文档-中英对照版.zip

    赠送Maven依赖信息文件:commons-io-2.11.0.pom; 包含翻译后的API文档:commons-io-2.11.0-javadoc-API文档-中文(简体)-英语-对照版.zip; Maven坐标:commons-io:commons-io:2.11.0; 标签:commons、jar包、java...

    commons-io-2.2-API文档-中文版.zip

    赠送jar包:commons-io-2.2.jar; 赠送原API文档:commons-io-2.2-javadoc.jar; 赠送源代码:commons-io-2.2-sources.jar; 包含翻译后的API文档:commons-io-2.2-javadoc-API文档-中文(简体)版.zip 对应Maven...

    commons-io-2.2

    commons-io-2.2 maven 依赖 jar包 commons-io-2.2maven 依赖 jar包 commons-io-2.2 commons-io-2.2

    commons-io-1.4-API文档-中文版.zip

    赠送Maven依赖信息文件:commons-io-1.4.pom; 包含翻译后的API文档:commons-io-1.4-javadoc-API文档-中文(简体)版.zip; Maven坐标:commons-io:commons-io:1.4; 标签:commons、io、中文文档、jar包、java; ...

    commons-io-2.2-API文档-中英对照版.zip

    赠送jar包:commons-io-2.2.jar 赠送原API文档:commons-io-2.2-javadoc.jar 赠送源代码:commons-io-2.2-sources.jar 包含翻译后的API文档:commons-io-2.2-javadoc-API文档-中文(简体)-英语-对照版.zip 对应...

    commons-io-2.4-API文档-中文版.zip

    赠送Maven依赖信息文件:commons-io-2.4.pom; 包含翻译后的API文档:commons-io-2.4-javadoc-API文档-中文(简体)版.zip; Maven坐标:commons-io:commons-io:2.4; 标签:commons、jar包、java、API文档、中文版; ...

    commons-io-2.11.0.rar

    commons-io-2.11.0.rar

    Java IO框架 commons-io-2.11.0

    Java IO框架 commons-io-2.11.0

Global site tag (gtag.js) - Google Analytics