通过在CREATE选项中添加DELETE_ON_CLOSE选项来模拟临时文件
import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; /** * 时代Java公众号 - nowjava.com 提 供 **/ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; public class Main { public static void main(String[] args) throws Exception { /* 时 代 J a v a 提 供 */ String tmp_file_prefix = "test_"; String tmp_file_sufix = ".txt"; Path tmp_file = null; tmp_file = FileSystems.getDefault().getPath("C:/folder1/tmp", tmp_file_prefix + "temporary" + tmp_file_sufix); try (OutputStream outputStream = Files.newOutputStream(tmp_file, StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE); BufferedWriter writer = new