Java file delete fails


 

Java file delete fails

This section demonstrates you how the deletion of file fails.

This section demonstrates you how the deletion of file fails.

Java file delete fails

This section demonstrates you how the deletion of file fails.

Description of code:

Sometimes you got an error that file deletion fails. There may be some reasons for this like the file you want to delete, does not exist or the file is already locked, or it is being used by another application. So before going to delete a file, check whether the file exists or not. If you make sure that file exists and again if deletion fails then it means that there is some handle open for the file or is being used by another program. So you need to close all the handles and then call the method delete().

In the give example, we have thrown an error by specifying wrong path. It has displayed 'File Deletion Fails'.

Here is the code:

import java.io.*;

public class FileDeleteFails {
	public static void main(String[] args) {
		File f = new File("C:/java/data.txt");
		boolean isDelete = f.delete();
		if (isDelete) {
			System.out.println("File is deleted");
		} else {
			System.out.println("File Deletion Fails");
		}
	}
}

Ads