How to delete a file or folder from directory in java
Use a method called delete() from java.io.File to delete a file. While doing this operation , we need to take care about the parent path name and the file name.
It returns true if the file or directory deleted successfully , otherwise it returns false.
It throws an IOException when a file or directory does not exist in the specified location .
|boolean <BooleanObjName> = <FileObject>.delete();|
Example to delete a file :
Author : java2db.com
public static void main(String args)
//deleting a file
File fileToDelete = new File("C:\\userfile\\file.txt");
System.out.println("File deleted successfully");
System.out.println("File not deleted successfully");
//deleting a directory
File dirToDelete = new File("C:\\userfile");//give the directory path
System.out.println("Directory deleted successfully");
System.out.println("Directory not deleted successfully");
File deleted successfully
Directory deleted successfully
If the specified directory having files, This method should not delete the directory.
|Related Posts :|
|How to Connect to Oracle from Liferay portlet ( Java Liferay and Oracle integration )|
|How to Connect Liferay 7 to PostgreSQL ( Java Liferay portlet and PostgreSQL integration )|
|How to call C# .Net WCF web service in java SOAP client ( Visual Studio 2013 )|
|How to call PHP RESTful web service in java client|
|How to call java SOAP web service in php ( php web service client )|
|Solution to SSLHandshakeException (skip the certificate for Java and LDAP bind)|
|Java RESTful web service example in maven using JAX-RS|
|RESTful web service example in java using JAX-RS|