1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
/* Create temporary file This Java example shows how to create a new temporary file using createTempFile method of Java File class. */ import java.io.*; public class CreateTemporaryFile { public static void main(String[] args) { /* * To create temporary file use, * static File createTempFile(String namePrefix, String nameSuffix) method * of Java File class, * * where namePrefix is a prefix string used to generate * a file's name and must be atleast 3 characters long and nameSuffix is a * suffix string used to generate suffix of the temporary file name. Suffix * may be null, and in that case default ".tmp" will be used as a suffix. */ File file1 = null; File file2 = null; try { //create temporary file wihtout extension suffix file1 = File.createTempFile("JavaTemp", null); //create temporary file with specified extension suffix file2 = File.createTempFile("JavaTemp", ".javatemp"); } catch(IOException ioe) { System.out.println("Exception creating temporary file : " + ioe); } /* * Temporary file will be created in default temporary directory of operating * system. */ System.out.println("Temporary file without suffix extension: " + file1.getPath()); System.out.println("Temporary file with suffix extension: " + file2.getPath()); } } /* Typical output would be Temporary file without suffix extension: C:TempJavaTemp45096.tmp Temporary file with suffix extension: C:TempJavaTemp45097.javatemp */ |
Add Comment