11:14 am - Tuesday October 24, 2017

Program for Creating StaticFile createTempFile(String prefix,String suffix,File directory)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 

The java.io.File.createTempFile(String prefix, String suffix, File directory)    method creates a new empty file in the specified directory.

EXAMPLE PROGRAM


import java.io.File;

public class CreatTempFile {

	public static void main(String[] args) {

		File f = null;

		try {

			f = File.createTempFile("tmp", ".txt", new File("E:/"));

			System.out.println("File path: " + f.getAbsolutePath());

			f.deleteOnExit();

			f = File.createTempFile("tmp", null, new File("F:/"));

			System.out.println("file path: " + f.getAbsolutePath());

			f.deleteOnExit();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


Output:

      File path: E:\tmp8740310890018082684.txt

file path: F:\tmp2942025563879391613.tmp

 Explanation:

The java.io.File.createTempFile(String prefix, String suffix, File directory)    method creates a new empty file in the specified directory.

Parameters:

prefix — The prefix string defines the files name; must be at least three characters long

suffix — The suffix string defines the file’s extension; if null the suffix “.tmp” will be used

directory — The directory in which the file is to be created. For default temporary-file directory null is to passed

Returns:  An abstract pathname for the newly-created empty file.

Throws:

  •    IllegalArgumentException — If the prefix argument contains less than three characters

IOException — If a file creation failed

    SecurityException — If SecurityManager.checkWrite(java.lang.String) method does not   allow a file to be created

 

BACK

 

Filed in: Z-A All Java Codes

Comments are closed.