zoukankan      html  css  js  c++  java
  • 【java】java删除文件delete和deleteOnExit 方法的区别,为什么调用deleteOnExit无效?

    delete()   是即刻删除

    public boolean delete() {
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                security.checkDelete(path);
            }
            if (isInvalid()) {
                return false;
            }
            return fs.delete(this);
        }
    
    
    最终调用native本地方法 立即进行删除

    deleteOnExit() 调用后,不会立即删除,会等到虚拟机正常运行结束后,才去删除

    public void deleteOnExit() {
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                security.checkDelete(path);
            }
            if (isInvalid()) {
                return;
            }
            DeleteOnExitHook.add(path);
        }
    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();
            }
        }
    View Code
  • 相关阅读:
    jquery弹出窗口
    js定时器
    jquery树形菜单
    用convert转换参数对比
    jquery常用例子!
    JS总结
    上传简历实现只浏览不下载的效果
    区块链入门(1):搭建(Ubuntu系统)Truffle v3.2.1 开发和测试环境
    URL Routing组件是如何与ASP.NET MVC框架组合起来的
    接单网站收集
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/10937041.html
Copyright © 2011-2022 走看看