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 |
/* Create temporary file in specified directory This Java example shows how to create a new temporary file at specified path using createTempFile method of Java File class. */ import java.io.*; public class CreateTempFileDirectory { public static void main(String[] args) { /* * To create temporary file at specified location use, * static File createTempFile(String namePrefix, String nameSuffix, File dir) 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, may be null, * and in that case default ".tmp" will be used as a suffix. dir is the * directory under which the temporary file will be created. */ File file = null; File dir = new File("C://FileIO"); try { file = File.createTempFile("JavaTemp", ".javatemp", dir); } catch(IOException ioe) { System.out.println("Exception creating temporary file : " + ioe); } /* * Please note that if the directory does not exists, IOException will be * thrown and temporary file will not be created. */ System.out.println("Temporary file created at : " + file.getPath()); } } /* Typical output would be Temporary file created at : C:FileIOJavaTemp40534.javatemp */ |
Add Comment