belongs to collection: Java Files & Directories Programs
This method accepts two arguments 1) file_name and 2) file_extension name. If the file_extension is null, the default file extension will be .tmp.
Package to use: import java.io.*;
Syntax:
//file object creation File one = null; File two = null; //temporary file creation one = File.createTempFile("file_name", "extension"); two = File.createTempFile("file_name", "extension");
// Java code to create a temporary file import java.io.*; 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); } } }
Output
file created: /tmp/sample14223766203657377545.javatemp file created: /tmp/sample22708977934903691208.tmp
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Java code to create a temporary file
Output