[JAVA] 특정폴더 하위의 모든 파일 삭제

[JAVA] 특정폴더 하위의 모든 파일 삭제

    public void deleteFilesInRootDir(File rootDir) {
        deleteFilesInRootDir(rootDir, rootDir);
    }
   
   
    private void deleteFilesInRootDir(File rootDir, File target) {
        if (rootDir == null) {
            return;
        }
       
        if (target == null || !target.exists()) {
            return;
        }
       
        if (target.isDirectory()) {
            File[] files = target.listFiles();
            for (File oneFile : files) {
                if (oneFile != null && oneFile.isFile() && oneFile.exists()) {
                    try {
                        oneFile.delete();
                        // System.out.println(“delete… [” + oneFile.getAbsolutePath() + “]”);
                    } catch (Exception e) {
                    }
                } else if (oneFile != null && oneFile.isDirectory()) {
                    deleteFilesInRootDir(rootDir, oneFile);
                }
            }
           
            // delete without root folder
            if (!rootDir.getAbsolutePath().equals(target.getAbsolutePath())) {
                try {
                    target.delete();
                    // System.out.println(“delete… [” + target.getAbsolutePath() + “]”);
                } catch (Exception e) {
                }
            }
        }
    }