Java文件夹递归删除方法(2)

/**//*
     * visit all a director and save them in a list
     */
    @SuppressWarnings("unchecked")
    private static void visitAll(java.io.File tempRoot) throws Exception ...{
        java.io.File[] dirs = tempRoot.listFiles();
        if (dirs != null) ...{
            List dirsList = Arrays.asList(dirs);
            if (dirsList == null) ...{
                try ...{
                    if(!tempRoot.delete())
                        throw new Exception("Exception: delete file " +
                                tempRoot.getName() + " false!");
                } catch (Exception e) ...{
                    throw e;
                }
            } else ...{
                m_dirs.addAll(dirsList);
                for (int i = 0; i < dirsList.size(); i++) ...{
                    tempRoot = (java.io.File) dirsList.get(i);
                    visitAll(tempRoot);
                }
            }
        }
    }

/**//*
     * do delete
     */
    private static void myDelete() throws Exception ...{
        visitAll(m_root);
        if (m_dirs != null) ...{
            for (int i = m_dirs.size() - 1; i >= 0; i--) ...{
                java.io.File f = (java.io.File) m_dirs.remove(i);
                String fileName = f.toString();
                if (!f.delete()) ...{
                    throw new Exception("Exception: delete file " + fileName + " false!");
                }
            }
        } else ...{
            throw new Exception("Exception: read file list of " + m_root.toString()
                    + "false!");
        }
    }
}

这个类参考了Java删除文件夹 (我参考的不是这篇文件,时间长了找不到原始文章,但是这篇文章类似)。

我在这里想说两个问题:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwpdsj.html