集册 Java实例教程 通过在“创建”选项中添加“关闭时删除”选项来模拟临时文件

通过在“创建”选项中添加“关闭时删除”选项来模拟临时文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

637
通过在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 
展开阅读全文