*
* 删除一个目录下的所有文件
*/
public static void delAllFile(String path) {
File file = new File(path);
if(!file.exists()) return;
if(!file.isDirectory()) return;
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]);
}
}
删除时确认
1. 你的File是文件还是文件夹,如果是文件夹,只能在其中没有文件时删除。
2. 你的程序有没有正在使用该文件,比如打开了Stream等而没关闭。
3. 有没有被操作系统中的其它程序使用,比如在WinWord中也打开了该文件。
* 删除一个目录下的所有文件
*/
public static void delAllFile(String path) {
File file = new File(path);
if(!file.exists()) return;
if(!file.isDirectory()) return;
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]);
}
}
删除时确认
1. 你的File是文件还是文件夹,如果是文件夹,只能在其中没有文件时删除。
2. 你的程序有没有正在使用该文件,比如打开了Stream等而没关闭。
3. 有没有被操作系统中的其它程序使用,比如在WinWord中也打开了该文件。