java删除文件及文件夹
2012-05-30 16:48
330 查看
/** * 删除指定文件夹下所有文件 * param path 文件夹完整绝对路径 * @param path * @return */ public static boolean delAllFile(String path) { boolean flag = false; File file = new File(path); if (!file.exists()) { return flag; } if (!file.isDirectory()) { return flag; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件 delFolder(path + "/" + tempList[i]);//再删除空文件夹 flag = true; } } return flag; } /** * 删除文件夹 * @param folderPath */ public static void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容 String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = new java.io.File(filePath); myFilePath.delete(); //删除空文件夹 } catch (Exception e) { e.printStackTrace(); } }
相关文章推荐
- java实现读取、删除文件夹下的文件
- java文件和文件夹复制、删除、移动操作
- Java删除文件夹和文件
- java 对文件的删除、剪切、移动和创建文件夹操作
- Java创建、重命名、删除文件和文件夹
- java读取文件、文件夹/java删除文件、文件夹
- java文件和文件夹复制、删除、移动操作
- java遍历删除windows下的指定文件夹下面的相同格式或指定格式的file文件
- java android 删除文件和文件夹的函数
- java删除文件夹 文件
- Java中使用File类删除文件夹和文件
- java 循环文件夹删除指定名称的文件
- JAVA删除文件夹和文件
- 【java删除指定文件夹或者文件】
- Java删除文件夹以及文件夹下的子目录与文件
- 用java删除文件夹里的所有文件
- Java中实现复制文件到文件,复制文件到文件夹,复制文件夹到文件,删除文件,删除文件夹,移动文件,移动文件夹的工具类
- ( 转载)纯Java文件操作工具,支持文件、文件夹的复制、删除、移动
- java删除文件和文件夹
- java正则匹配指定文件名称动态删除(保留最新的三个文件夹)升级版