Share on Google+Share on Google+

Java file delete fails

Advertisement
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");
		}
	}
}

Advertisements

Advertisement

Posted on: June 17, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.