解説動画
前回のコピー同様、サブフォルダを含んだ一括削除はそのままできないので、再帰的に行います。
 こちらもメソッドで再帰せず、クラスで再帰してます。
■動画はこちら
■Youtube版の解説で使用しているソースコード
 動画と一緒にこちらも参考にどうぞ。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileListMain {     public static void main(String[] args) {         try {             //削除するフォルダのパス             String deletePathName = "R:\\work\\test_File2";             //フォルダ内の削除             FileList fileList = new FileList(deletePathName);             fileList.delete();             //最後に自分のフォルダを削除             Path filePath = Paths.get(deletePathName);             Files.delete(filePath);         } catch (IOException e) {             e.printStackTrace();         }     } } | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileList {     private String currentPath = null;     private File[] files = null;     public FileList(String path) {         currentPath = path;         File file = new File(path);         files = file.listFiles();     }     public void report() {         for(File f : files) {             reportFile(f);         }     }     private void reportFile(File file) {         //自分のファイル(フォルダ)の情報を出力する         //ファイル名の取得         String fileName = currentPath + File.separator + file.getName();         //ファイルの情報を出力         System.out.println(fileName);         //フォルダの場合は、さらに自分のサブフォルダを見る         if(file.isDirectory()) {             FileList fileList = new FileList(fileName);             fileList.report();         }     }     public void copy(String destPathName) throws IOException {         for(File f : files) {             copyFile(f, destPathName);         }     }     private void copyFile(File file, String destPathName) throws IOException {         //自分のファイル(フォルダ)をコピーする         //自分のファイル名の取得         String fileName = currentPath + File.separator + file.getName();         //ファイルをコピー(フォルダの場合、フォルダだけ作成)         Path srcPath = Paths.get(fileName);         Path destPath = Paths.get(destPathName + File.separator + file.getName());         Files.copy(srcPath, destPath);         //フォルダの場合は、さらに自分のサブフォルダを見る         if(file.isDirectory()) {             FileList fileList = new FileList(fileName);             fileList.copy(destPathName + File.separator + file.getName());         }     }     public void delete() throws IOException {         for(File f : files) {             deleteFile(f);         }     }     private void deleteFile(File file) throws IOException {         //自分のファイル(フォルダ)を削除する         //自分のファイル名の取得         String fileName = currentPath + File.separator + file.getName();         //フォルダの場合は、さらに自分のサブフォルダを見る         if(file.isDirectory()) {             FileList fileList = new FileList(fileName);             fileList.delete();         }         //自分のフォルダ・ファイルを削除         Path filePath = Paths.get(fileName);         Files.delete(filePath);     } } | 














