7:58 am - Friday March 24, 2017

Program for Creating empty file in abstract pathname using boolean createNewFile()

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

This   Program for Creating empty file in abstract pathname using  boolean createNewFile()

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

 

The File Created:false//already exist

The File Created:true

Explanation

 

public boolean createNewFile()throws IOException

 

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

 

Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

 

Returns:

   true if the named file does not exist and was successfully created; false if the named file already exists

 

Throws:

   IOException – If an I/O error occurred

   SecurityException – If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the file

 

Since:

   1.2

BACK

Filed in: Z-A All Java Codes

Comments are closed.