Home Tutorial Java Core Files Java file delete fails

 
 

Java file delete fails
Posted on: June 17, 2006 at 12:00 AM
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");
		}
	}
}

Related Tags for Java file delete fails:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.