windows中默认的文件名和路径名是有长度限制的。过长了就出错了,但有时候不小心系统中就多出来较深层次的目录。如下图所示:
java程序中,一般会递归调用删除方法来删除文件和目录,但递归的层次是有限制的,一般会出错而无法删除较深目录。
本文通过在递归模式上稍做修改,实现了较深层次的目录的删除。具体代码如下:
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
/**
* @Author ...
* @Date 2017.10.1
*
*/
public class Delete {
public static final int MAX_DOT_NUM = 30;
public static List<File> fileList = new ArrayList<File>();
public static void main(String[] args) {
if( args.length < 1 ){
System.out.println("实现功能:删除指定的文件、文件夹及其包含的文件和子目录列表(对Windows操作系统不支持的层次较深的目录也有效)。");
System.out.println();
System.out.println("命令格式:java Delete [filename | pathname]");//[drive:][path][filename]
System.out.println("\tfilename\t文件名");
System.out.println("\tpathname\t目录名");
System.out.println();
System.out.println("示例:");
System.out.println("\tjava Delete d:\\test.txt");
System.out.println("\tjava Delete \"d:\\hello world\"");
System.out.println("\t说明:文件夹包含空格符号时,请用引号将文件名或目录名括起来");
System.out.println();
return;
}
try{
System.out.print("开始处理,请耐心等待...");
long startTime=System.currentTimeMillis();
int count = delete(args[0]);
long stopTime=System.currentTimeMillis();
System.out.print("处理结束,处理文件(或目录)" + count + "个,");
System.out.println("花费时长:"+((stopTime-startTime)/1000)+"秒");
}catch(Exception ex){
ex.printStackTrace();
}
}
/**
* 删除指定名称的文件或文件夹(包含其子文件夹和文件)
*
* @param filename 文件或文件夹名称
* @throws IOException
*/
public static int delete(String filename) throws IOException{
int count = 0;
boolean bPlayMovies = true;//设为false时关闭删除时的动画;
File f = new File(filename);
if( !f.exists() ){
System.out.println( "\r\n指定的文件或文件夹" + f + "不存在!");
return 0;
}
fileList.add(f);
boolean isplus = false;
while( fileList.size()>0 ){
if( bPlayMovies ){
if( count % MAX_DOT_NUM == 0 ){
isplus = !isplus;
}
}
File tmp = fileList.get(fileList.size()-1);
if( tmp.isDirectory() ){
//处理文件夹
File[] files = tmp.listFiles();
if( files.length > 0 ){
//如果该文件夹下包含文件或子文件夹
for(File file : files){
fileList.add(file);
}
} else{
fileList.remove(fileList.size()-1);
//System.out.println("删除文件:"+tmp);
tmp.getAbsoluteFile().delete();
count++;
if( bPlayMovies ){
playMovies( isplus );
}
}
}else{
//处理文件
fileList.remove(fileList.size()-1);
//System.out.println("删除文件:"+tmp);
tmp.delete();
count++;
if( bPlayMovies ){
playMovies( isplus );
}
}
}
if( bPlayMovies ){
playMovies( false );
}
System.out.println();
return count;
}
public static void playMovies(boolean isplus){
if( isplus ){
System.out.print(".");
}else{
for( int j = MAX_DOT_NUM; j > 0; j--){
System.out.print("\b \b");
}
}
}
}
编译后,执行“java Delete 待删除的目录”。
较深的目录生成方法如下:
import java.io.File;
import java.lang.Integer;
/**
* 生成指定深度的文件夹
*
*/
public class CreateFolders {
public static void main(String[] args) {
int depth = 3100;//控制创建文件夹的深度
try{
depth = Integer.valueOf(args[0]);
}catch(Exception ex){
}
StringBuffer fileName = new StringBuffer();
fileName.append("D:\\test");
for(int i=1; i<= depth; i++){
File file = new File(fileName.toString());
if( !file.exists() ){
file.mkdirs();
}
fileName.append("\\").append("hello");
//System.out.print("\b\b\b"+(int)((i*1.0/depth)*100)+"%");
System.out.printf("\b\b\b\b\b\b%2.0f%%",i*1.0/depth*100);
}
System.out.print("\b\b\b\b\b\b100% \r\n");
System.out.println("指定驱动器/目录下生成的文件夹深度为:" + depth);
}
}