删除文件或者文件夹
来源:互联网 发布:广电机顶盒安装软件 编辑:程序博客网 时间:2024/06/04 18:45
import java.io.File;
public class Test
{
public static void main(String args[]){
Test t = new Test();
delFolder("c:/bb");
System.out.println("deleted");
}
//删除文件夹
//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();
}
}
//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
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;
}
}
{
public static void main(String args[]){
Test t = new Test();
delFolder("c:/bb");
System.out.println("deleted");
}
//删除文件夹
//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();
}
}
//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
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;
}
}
- 删除文件或者文件夹
- java删除文件夹或者文件
- git删除文件或者文件夹
- java删除指定文件夹或者文件
- Java 文件或者文件夹的删除
- myeclipse svn 删除文件或者文件夹
- Android 文件或者文件夹删除工具类
- Java删除指定文件或者文件夹
- 根据文件或者文件夹名批量删除文件或文件夹
- 删除指定文件夹下的文件或者文件夹
- 利用短名来删除windows下的不合法文件或者文件夹
- SD卡指定路径删除文件或者文件夹
- C#删除指定目录的文件夹或者文件
- github删除远程分支上的文件或者文件夹
- git 删除远程已经推送过的文件或者文件夹
- 删除当前目录下指定文件或者文件夹的批处理
- git 删除远程仓库中的文件或者文件夹
- asp.net 删除文件夹内的所有文件 或者单个文件 代码
- 各种IO模型的讨论总结
- 又犯错了
- 用线程函数来访问一个类
- 一个程序员的六个阶段 (转)
- 乔布斯给年轻人的十句金玉良言
- 删除文件或者文件夹
- 是否愿意使用WIN7?
- 免安装Oracle客户端使用PL/SQL连接Linux Oracle 注意事项
- 代理模式
- 性能监控的简单实现
- 中国弱智现象大集合
- connect by应用
- 常用DOS命令讲解
- linux shell学习笔记1