Tuesday, March 8, 2016

Java I/O Examples : How To Create A Temporary File

Intro


To create a temporary file in Java, you can use createTempFile() method in the java.io.File class.

Example


package com.freesamplecode.java.io;

import java.io.File;
import java.io.IOException;

public class CreateTemporaryFileDemo {
 public static void main(String[] args){
  File tempFile = null;
  try {
    tempFile = File.createTempFile("tempfile", "tmp");
    
    System.out.println("Successfully created temp file in "+tempFile.getAbsolutePath());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

Output


Successfully created temp file in C:\Users\Dev\AppData\Local\Temp\tempfile5993040830414702074tmp

Related Posts:

0 comments: