JAVA删除某一目录下的所有文件及文件夹(linnux环境)

//删除dashboard/Schema目录下的xml文件 , 没使用
public static String deleteExistFile(String folderPath){
//获得Tomcat安装目录下的temp文件夹的绝对路径
String flashPath = System.getProperty("catalina.home")+ " \\temp\\";
System.out.println("+++++=pathName: "+flashPath+"\n+++++folderPath: " + folderPath);
folderPath += " \\dashboard/Schema";
try{
SchemaMaker.delAllFile(folderPath);
//SchemaMaker.delAllFile(flashPath);
return flashPath;
}catch(Exception e){
System.out.println("SchemaMaker.deleteExistFile throw exception ! \n删除dashboard/Schema目录下的xml文件失败!");
return flashPath;
}
}


public static void delAllFile(String folderPath){
try{
File file = new File(folderPath);
if(file.isDirectory() && file.listFiles().length > 5){ //文件数量大于5,即执行删除
//if(file.isDirectory()){ //是文件夹
File temp = null;
String [] childsFile = file.list(); //获得该目录下的子文件及子文件夹
for(String s : childsFile){
//检查folderPath是不是以"\"节尾
if(folderPath.endsWith(file.separator)){
temp = new File(folderPath+s);
}else{
temp = new File(folderPath+file.separator+s);
}
//delete file
if(temp != null && temp.isFile()){ //是文件
//去除文件的只读属性
//SchemaMaker.clearReadonly(temp.getAbsolutePath());
//如果项目布署在\Tomcat 5.5\webapps用,删除有效,反之无效
temp.delete();
//如果项目以xml形式布署在Tomcat 5.5\conf\Catalina\localhost用,删除有效,反之无效
SchemaMaker.runtimeDelFile(temp.getAbsolutePath());
System.out.println("delete of the fileName: " + temp.getAbsolutePath());
}else if(temp != null && temp.isDirectory()){ //是文件夹
SchemaMaker.delFolder(temp.getAbsolutePath());
}
}
}else if(file.isFile()){ //是文件
file.delete();
}
}catch(Exception e){
System.out.println("Schema.delAllFile throw exception !");
e.printStackTrace();
}
}


public static void clearReadonly(String absolutePath){
try {
Runtime.getRuntime().exec("cmd /c attrib "+absolutePath+" -r -a -s -h");
System.out.println("文件: "+absolutePath+" 已去除只读属性!");
try {
  Thread.sleep(1000); //程序休眠一秒钟
  } catch (InterruptedException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
  }
}catch(IOException e) {
e.printStackTrace();
}
}

//使用运行时命令 cmd /c del 加文件绝对路径,删除文件
public static void runtimeDelFile(String absolutePath){
Runtime rt = Runtime.getRuntime();
try {
rt.exec("cmd /c del " + absolutePath);
} catch (IOException e) {
System.out.println("Schema.runtimeDelFile throw exception ! ");
e.printStackTrace();
}
}


public static void delFolder(String folderPath) {
try {
SchemaMaker.delAllFile(folderPath); // 删除完里面所有内容
File rootFilePath = new File(folderPath);
//if(rootFilePath.isDirectory() && rootFilePath.listFiles().length > 0){
if(rootFilePath.isDirectory() && rootFilePath.listFiles().length > 5){ //文件数量大于5,即执行删除
  SchemaMaker.delFolder(rootFilePath.getAbsolutePath());
}else{
  rootFilePath.delete(); // 删除空文件夹
//SchemaMaker.runtimeDelFile(rootFilePath.getAbsolutePath()); // 删除空文件夹,不能执行
}
} catch (Exception e) {
System.out.println("SchemaMaker.delFolder throw exception");
  e.printStackTrace();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值