Java Temp File Create nio createTempFile()

Here you can find the source of createTempFile()

Description

create Temp File

License

Open Source License

Declaration

public static Path createTempFile() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import java.nio.file.StandardOpenOption;

public class Main {
    public static Path createTempFile() {
        return createTempFile(null);
    }//from  w w  w .j  av  a  2s  .c  om

    public static Path createTempFile(String content) {
        try {
            Path tmpFile = Files.createTempFile("", null);

            if (content == null)
                Files.write(tmpFile, tmpFile.toString().getBytes("utf-8"), StandardOpenOption.CREATE,
                        StandardOpenOption.TRUNCATE_EXISTING);
            else
                Files.write(tmpFile, content.getBytes("utf-8"), StandardOpenOption.CREATE,
                        StandardOpenOption.TRUNCATE_EXISTING);

            return tmpFile;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

Related

  1. createTempFile(final Object forObject)
  2. createTempFile(final String name)
  3. createTempFile(String contents)
  4. createTempFile(String name)