Creating temporary files in Java

Posted: 9th July 2015 by Tim in Java
Tags: , , , , ,

Sometimes it is necessary to use temporary files in your program. One concern when creating these files is thread safety; two processes creating a temporary file with the same name. This concern can be largely ignored using Java’s File.createTempFile, which guarantees the file path is unique amongst all java processes running on that JVM.

For example:


class TempFile {
    public static void main(String[] args) throws IOException {
        File tempFile = File.createTempFile("prefix_", "_suffix");

will print something like: