FileUtils { public static boolean cleanDirectory(File dir) { if (dir.isDirectory()) { String[] children = dir.list(); if (children != null) { for (String aChildren : children) { boolean success = cleanDirectory(new File(dir, aChildren)); if (!success) { return false; } } } } return dir.delete(); }  static String getBaseDirName(); static String getUserHomeDir(String base); static String file2String(File file); static String file2String(Class clazz, String relativePath, String encoding); static List<String> readLines(File file); static boolean string2File(File file, String data); static boolean cleanDirectory(File dir); static final String LINE_SEPARATOR; }
@Test public void cleanDirectory() throws Exception { String filePath = System.getProperty("java.io.tmpdir") + File.separator + "FileTest" + 1; FileUtils.string2File(new File(filePath, "xx.tmp"), "helloworld!"); Assert.assertTrue(new File(filePath, "xx.tmp").exists()); String ct = FileUtils.file2String(new File(filePath, "xx.tmp")); Assert.assertTrue(ct.equals("helloworld!")); List<String> datas = FileUtils.readLines(new File(filePath, "xx.tmp")); Assert.assertTrue(datas.size() == 1); FileUtils.cleanDirectory(new File(filePath)); Assert.assertFalse(new File(filePath).exists()); }