2:16 am - Tuesday October 24, 2017

Program for Creating Temorary Empty files using createTempFile(String prefix, String suffix)

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

Program for Creating Temorary Empty files using createTempFile(String prefix, String suffix)

EXAMPLE PROGRAM


import java.io.*;

public class FileCreate {

	public static void main(String[] args) {

		File f = null;

		boolean bool = false;

		try {

			f = new File("abc.txt");

			bool = f.createNewFile();

			System.out.println("The File Created:" + bool);

			f.delete();

			bool = f.createNewFile();

			System.out.println("The File Created:" + bool);

		} catch (Exception e) {

			System.out.println(e);

		}

	}

}

Output

C:\DOCUME~1\bala\LOCALS~1\Temp\test43662.txt

C:\DOCUME~1\bala\LOCALS~1\Temp\test43663.tmp

 

Explanation

public static File createTempFile(String prefix,String suffix)throws IOException

Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking createTempFile(prefix, suffix, null).

The Files.createTempFile method provides an alternative method to create an empty file in the temporary-file directory. Files created by that method may have more restrictive access permissions to files created by this method and so may be more suited to security-sensitive applications.

 

Parameters:

   prefix – The prefix string to be used in generating the file’s name; must be at least three characters long

   suffix – The suffix string to be used in generating the file’s name; may be null, in which case the suffix “.tmp” will be used

Returns:

   An abstract pathname denoting a newly-created empty file

Throws:

   IllegalArgumentException – If the prefix argument contains fewer than three characters

   IOException – If a file could not be created

   SecurityException – If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created

Since:

   1.2

See Also:

   Files.createTempDirectory(String,FileAttribute[])

BACK

Filed in: Z-A All Java Codes

Comments are closed.