LoginSignup
3
3

More than 5 years have passed since last update.

【Java】フォルダ内の指定した拡張子を一括削除する方法

Posted at

try-with-resourcesを使ったりNew I/O 2を使ったりしてるのでJDK1.7以上じゃないと使えません。


// 指定フォルダをhogeとする。
Path path = Paths.get("C:","hoge");
try(DirectoryStream<Path> ds = Files.newDirectoryStream(path, "*.txt") ){
    for(Path deleteFilePath : ds){
        System.out.println("次のファイルを削除します。: " + deleteFilePath.toString());
        Files.delete(deleteFilePath);
    }
} catch (IOException e) {
    e.printStackTrace();
}
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3