is there an existing FileInputStream delete on close?

前端 未结 4 962
夕颜
夕颜 2020-12-15 16:52

Is there an existing way to have a FileInputStream delete the underlying file automatically when closed?

I was planning to make my own utility class t

4条回答
  •  死守一世寂寞
    2020-12-15 17:47

    I know this is a fairly old question; however, it's one of the first results in Google, and Java 7+ has this functionality built in:

    Path path = Paths.get(filePath);
    InputStream fileStream = Files.newInputStream(path, StandardOpenOption.DELETE_ON_CLOSE);
    

    There are a couple caveats with this approach though, they're written up here, but the gist is that the implementation makes a best effort attempt to delete the file when the input stream is closed, and if that fails makes another best effort attempt when the JVM terminates. It is intended for use with temp files that are used solely by a single instance of the JVM. If the application is security sensitive, there are also a few other caveats.

提交回复
热议问题