package org.accp.io.demo;
import java.io.File;
import java.io.FileFilter;
import java.math.MathContext;
public class Demo1 {
public static void main(String[] args) {
//showFiles2("C://Documents and Settings//Administrator.JIANG//桌面//MyPlayer","h");
//showFiles3("C://Documents and Settings//Administrator.JIANG//桌面//MyPlayer","cpp");
//createDir("c://a//b//c//d");
deleteDir("c://a//b//c//d");
}
private static void iterate(File...files){
for(File f: files){
System.out.println((f.isDirectory()?"目录:/t":"文件:/t")+f.getName()+"/t"+(f.isFile()?"["+Math.round(f.length()/1024.0*100)/100.0+"KB]":"")+"");
}
}
/**显示指定目录下的所有子目录和文件*/
public static void showFiles1(String targetDirectory){
java.io.File file = new File(targetDirectory);
if (!file.exists())//如果目录不存在则返回
return;
iterate(file.listFiles());
}
/**显示指定目录下的所有子目录和文件[带过滤器]*/
public static void showFiles2(String targetDirectory,String extName){
java.io.File file = new File(targetDirectory);
if (!file.exists())//如果目录不存在则返回
return;
iterate(file.listFiles(new MyFileFilter(extName)));
}
public static void createDir(String targetDirectory){
java.io.File file = new File(targetDirectory);
if (!file.exists()){
file.mkdirs();
System.out.println(file.exists()?"创建成功":"创建失败");
}
}
public static void deleteDir(String targetDirectory){
java.io.File file = new File(targetDirectory);
if (file.exists()){
file.delete();
System.out.println(!file.exists()?"删除成功":"删除失败");
}
}
/**显示指定目录下的所有子目录和文件[带过滤器]*/
public static void showFiles3(String targetDirectory,final String extName){
java.io.File file = new File(targetDirectory);
if (!file.exists())//如果目录不存在则返回
return;
File[] files = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.getName().endsWith(extName);
}
});
iterate(files);
}
}
class MyFileFilter implements FileFilter{
private String extName;
public MyFileFilter(String extName) {
// TODO Auto-generated constructor stub
this.extName = extName;
}
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(extName);
}
}
s