java.nio.file.Files.deleteIfExists()のサンプル
サンプルコード
package com.arkgame.itstudy;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class DeleteFileDemo {
public static void main(String[] args) {
String dir = "C:\\datac";
String filename = "test.csv";
// ファイルが存在する場合は削除します
try {
Files.deleteIfExists(Paths.get(dir, filename));
} catch (IOException e) {
e.printStackTrace();
}
// フォルダを削除します
try {
Files.deleteIfExists(Paths.get(dir));
} catch (IOException e) {
e.printStackTrace();
}
}
}