12:53 am - Thursday July 27, 2017

Program used to delete the files or directory using Boolean Delete() method

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.delete() method deletes the file or directory defined by the abstract path name. To delete a directory, the directory must be empty.

EXAMPLE PROGRAM


import java.io.File;

public class BooleanDelete {
	public static void main(String[] args) {

		File f = null;
		Boolean bool = false;
		try {
			f = new File("test.txt");
			bool = f.delete();
			System.out.println("file deleted: " + bool);
			f.createNewFile();
			System.out.println("create new file method is invoked");
			bool = f.delete();
			System.out.println("file deleted:" + bool);

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

	}
}

Output: file deleted: false

create new file method is invoked

file deleted:true

Explanation:

The java.io.File.delete() method deletes the file or directory defined by the abstract path name. To delete a directory, the directory must be empty.

Parameters:  NA

Returns:

This method returns true if the file is successfully deleted, else false.

Throws:

securityException — If SecurityManager.checkWrite(java.lang.String) method denies   delete access to the file

Filed in: Z-A All Java Codes

Comments are closed.