Java文件操作,删除文件及文件夹

1.删除文件夹下的所有文件,所有文件夹,及删除本身

 

[html][/html] view plaincopy

  1. public static boolean deleteFolders(String filePath){
  2.         boolean isDeleteSuccess = false;
  3.         LinkedList<String> folderList = new LinkedList<String>();
  4.         folderList.add(filePath);
  5.         while(folderList.size() > 0){
  6.             File file1 = new File(folderList.poll());
  7.             File[] files1 = file1.listFiles();
  8.             ArrayList<File> fileList = new ArrayList<File>();
  9.             for(int i = 0; i < fileList.size(); i++){
  10.                 if(files1[i].isDirectory()){
  11.                     folderList.add(files1[i].getPath());
  12.                 }else{
  13.                     fileList.add(files1[i]);
  14.                 }
  15.             }
  16.             //删除文件
  17.             for(File file : fileList){
  18.                 file.delete();
  19.             }
  20.         }
  21.         //删除文件夹
  22.         folderList = new LinkedList<String>();
  23.         folderList.add(filePath);
  24.         while(folderList.size() > 0){
  25.             File file2 = new File(folderList.getLast());
  26.             if(file2.delete()){
  27.                 folderList.removeLast();
  28.             }else{
  29.                 File[] files2 = file2.listFiles();
  30.                 for(int i = 0; i < files2.length; i++){
  31.                     folderList.add(files2[i].getPath());
  32.                 }
  33.             }
  34.         }
  35.         if(folderList.size() == 0) isDeleteSuccess = true;
  36.         return isDeleteSuccess;
  37.     }

2.删除文件夹下的所有文件夹

 

[html][/html] view plaincopy

  1. public static boolean deleteUnderAllFolder(String filePath){
  2.         boolean isDeleteSuccess = false;
  3.         LinkedList<String> folderList = null;
  4.         File superFile = new File(filePath);
  5.         File[] files1 = superFile.listFiles();
  6.         for(int i = 0; i < files1.length; i++){
  7.             if(files1[i].isDirectory()){
  8.                 if(!files1[i].delete()){
  9.                     //取得所有文件的路径
  10.                     folderList = new LinkedList<String>();
  11.                     folderList.add(files1[i].getPath());
  12.                     while(folderList.size() > 0){
  13.                         File file = new File(folderList.poll());
  14.                         File[] files2 = file.listFiles();
  15.                         ArrayList<File> fileList = new ArrayList<File>();
  16.                         for(int j = 0; j < files2.length; j++){
  17.                             if(files2[j].isDirectory()){
  18.                                 folderList.add(files2[j].getPath());
  19.                             }else{
  20.                                 fileList.add(files2[j]);
  21.                             }
  22.                         }
  23.                         //删除所有文件
  24.                         for(File f : fileList){
  25.                             f.delete();
  26.                         }
  27.                     }
  28.                     //删除所有文件夹
  29.                     folderList = new LinkedList<String>();
  30.                     folderList.add(files1[i].getPath());
  31.                     while(folderList.size() > 0){
  32.                         File file = new File(folderList.getLast());
  33.                         if(file.delete()){
  34.                             folderList.removeLast();
  35.                         }else{
  36.                             File[] files = file.listFiles();
  37.                             for(int a = 0; a < files.length; a++){
  38.                                 folderList.add(files[a].getPath());
  39.                             }
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         if(folderList != null && folderList.size() == 0) isDeleteSuccess = true;
  46.         return isDeleteSuccess;
  47.     }

3.清空文件夹

 

[html][/html] view plaincopy

  1. public static boolean emptyFolder(String filePath){
  2.         File delifilefolder = new File(filePath);
  3.         if(delifilefolder.exists() && !delifilefolder.delete()){
  4.             LinkedList<String> folderList = new LinkedList<String>();
  5.             folderList.add(filePath);
  6.             while(folderList.size() > 0){
  7.                 File file1 = new File(folderList.poll());
  8.                 File[] files1 = file1.listFiles();
  9.                 ArrayList<File> fileList = new ArrayList<File>();
  10.                 for(int i = 0; i < fileList.size(); i++){
  11.                     if(files1[i].isDirectory()){
  12.                         folderList.add(files1[i].getPath());
  13.                     }else{
  14.                         fileList.add(files1[i]);
  15.                     }
  16.                 }
  17.                 //删除文件
  18.                 for(File file : fileList){
  19.                     file.delete();
  20.                 }
  21.             }
  22.             //删除文件夹
  23.             folderList = new LinkedList<String>();
  24.             folderList.add(filePath);
  25.             while(folderList.size() > 0){
  26.                 File file2 = new File(folderList.getLast());
  27.                 if(file2.delete()){
  28.                     folderList.removeLast();
  29.                 }else{
  30.                     File[] files2 = file2.listFiles();
  31.                     for(int i = 0; i < files2.length; i++){
  32.                         folderList.add(files2[i].getPath());
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.         return delifilefolder.mkdir();
  38.     }

标签