java刪除檔案 圖片 與檔案夾目錄與檔案

來源:互聯網
上載者:User

/**
* 刪除單個檔案
* @param filename 要刪除的檔案的檔案名稱
* @return 單個檔案刪除成功返回true,否則返回false
*/
public static boolean deletefile(string filename) {
file file = new file(filename);
// 如果檔案路徑所對應的檔案存在,並且是一個檔案,則直接刪除
if(file.exists() && file.isfile()) {
if(file.delete()) {
system.out.println("刪除單個檔案" + filename + "成功!");
return true;
} else {
system.out.println("刪除單個檔案" + filename + "失敗!");
return false;
}
} else {
system.out.println("刪除單個檔案失敗:" + filename + "不存在!");
return false;
}
}

刪除圖片

package com.data.maint;
  import java.io.file;
  import org.apache.log4j.logger;
  public class cleartempimg {
  private static final logger logger = logger.getlogger(cleartempimg.class);
  private final static string fs = system.getproperty("file.separator");
  private static string tomcat_home = system.getproperty("catalina.home");
  private static string path = "temp";
  private static final string temp_path = tomcat_home + fs + path + fs;
  private static boolean isrunning = false;
  public void run() {
  if (!isrunning) {
  logger.debug("刪除/www.111cn.net/temp/目錄下到期圖片執行開始...");
  isrunning = true;
  logger.debug("開始執行刪除tomcat下temp檔案夾png檔案任務");
  logger.debug("要刪除圖片檔案所在路徑為" + temp_path);
  file filetemp = new file(temp_path);
  // 判斷檔案是否存在
  boolean falg = false;
  falg = filetemp.exists();
  if (falg) {
  logger.debug("temp檔案存在");
  if (true == filetemp.isdirectory()) {
  logger.debug("temp檔案是個目錄");
  string[] png = filetemp.list();
  for (int i = 0; i < png.length; i++) {
  if (true == png[i].endswith("png")) {
  file file = new file(temp_path + fs + png[i]);
  if (true==file.isfile()) {
  boolean flag = false;
  flag = file.delete();
  if (flag) {
  logger.debug("成功刪除無效圖片檔案:" + file.getname());
  }
  }
  }
  }
  }
  } else {
  logger.debug("未找到tomcat/temp檔案夾,執行失敗,請手動刪除temp下所有png檔案");
  }
  isrunning = false;
  logger.debug("刪除/www.111cn.net/temp/目錄下到期圖片執行結束...");
  }
  }
  }


 

來個更複雜一點的

/**
* 刪除目錄及目錄下的檔案
* @param dir 要刪除的目錄的檔案路徑
* @return 目錄刪除成功返回true,否則返回false
*/
public static boolean deletedirectory(string dir) {
// 如果dir不以檔案分隔字元結尾,自動添加檔案分隔字元
if(!dir.endswith(file.separator))
dir = dir + file.separator;
file dirfile = new file(dir);
// 如果dir對應的檔案不存在,或者不是一個目錄,則退出
if((!dirfile.exists()) || (!dirfile.isdirectory())) {
system.out.println("刪除目錄失敗:" + dir + "不存在!");
return false;
}
boolean flag = true;
// 刪除檔案夾中的所有檔案包括子目錄
file[] files = dirfile.listfiles();
for(int i = 0; i < files.length; i++) {
// 刪除子檔案
if(files[i].isfile()) {
flag = deletefileutil.deletefile(files[i].getabsolutepath());
if(!flag)
break;
}
// 刪除子目錄
else if(files[i].isdirectory()) {
flag = deletefileutil.deletedirectory(files[i].getabsolutepath());
if(!flag)
break;
}
}
if(!flag) {
system.out.println("刪除目錄失敗!");
return false;
}
//刪除目前的目錄
if(dirfile.delete()) {
system.out.println("刪除目錄" + dir + "成功!");
return true;
} else {
return false;
}
}

public static void main(string[] args) {
// 刪除單個檔案
string file = "c:/test/www.111cn.net/test1/test2/testfile.txt";
deletefileutil.deletefile(file);
system.out.println();
// 刪除一個目錄
string dir = "c:/mb.111cn.net/test0/test1";
deletefileutil.deletedirectory(dir);
system.out.println();
// 刪除檔案
dir = "c:/test/test0";
deletefileutil.delete(dir);

}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.