Create a Temporary File in Java

Create a temporary file in Java

// Java code to create a temporary file  


public class Main {

  public static void main(String[] args) {
    //file object creation to store results
    File one = null;
    File two = null;

    try {
      //creating temporary file1 with extension '.javatemp'
      one = File.createTempFile("sample1", ".javatemp");

      //creating temporary file2 without using null
      two = File.createTempFile("sample2", null);

      System.out.println("file created: " + one.getPath());
      System.out.println("file created: " + two.getPath());

    } catch (IOException exp) {
      System.out.println("Error in creating temporary file: " + exp);


file created: /tmp/sample14223766203657377545.javatemp
file created: /tmp/sample22708977934903691208.tmp
Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment