├
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileFilter;
public class Folder{
private static FolderFilter folderFilter;
private static FilesFilter filesFilter;
public static void main(String[] args){
folderFilter=new FolderFilter();
filesFilter=new FilesFilter();
switch(args.length){
case 0:tree(".");break;
case 1:tree(args[0]);break;
case 2:
if(args[0].toLowerCase().equals("add")){
//添加新文件夹
}
else if(args[0].toLowerCase().equals("del")){
//删除文件夹
}
tree(".");
break;
}
}
public static void tree(String path){
try{
System.out.println(new File(path).getCanonicalPath());
doTree(new File(path),"");
}
catch(IOException ex){
ex.printStackTrace();
}
}
private static void doTree(File parent,String prefix){
File[] subItems=parent.listFiles(folderFilter);
int count=parent.listFiles().length;
for(int i=0;i<subItems.length;i++){
if(i==count-1){
System.out.println(prefix+"└"+subItems[i].getName());
doTree(subItems[i],prefix.concat(" "));
}
else{
System.out.println(prefix+"├"+subItems[i].getName());
doTree(subItems[i],prefix.concat("│"));
}
}
subItems=parent.listFiles(filesFilter);
for(int i=0;i<subItems.length;i++){
if(i==subItems.length-1){
System.out.println(prefix+"└"+subItems[i].getName());
}
else{
System.out.println(prefix+"├"+subItems[i].getName());
}
}
}
}
class FolderFilter implements FileFilter{
public boolean accept(File file){
return file.isDirectory();
}
}
class FilesFilter implements FileFilter{
public boolean accept(File file){
return file.isFile();
}
}