Delete Files In Java

The analysis on Java Files Delete is carried out with the intention of improving student’s learning with examples.



Java Files Delete

Use Java’s delete() method to remove a file.

Firstly, we remove the file named “FirstFile.txt” which we created and read in the prior chapters.

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File ample=new File("FirstFile.txt");if(ample.delete()){System.out.println(ample.getName()+" is successfully deleted from your system"); } else{ System.out.println("An error is generated during the removal of this file"); } } }

The outcome will be:

Java Files Delete examples

Correspondingly, using the same method remove() the file named SecondFile.txt is removed as shown in the example below:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File ample=new File("SecondFile.txt");if(ample.delete()){System.out.println(ample.getName()+" is successfully deleted from your system"); } else{ System.out.println("An error is generated during the removal of this file"); } } }

Delete PDF File:

Similarly, we can also delete the .pdf file, which we created in Java Create and Read File:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File ample=new File("NewFile.pdf");if(ample.delete()){System.out.println(ample.getName()+" is successfully deleted from your system"); } else{ System.out.println("An error is generated during the removal of this file"); } } }

Delete CSV File

In the same way, we can also delete the .csv file we created in Java Create and Read File:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File ample=new File("NewFile.csv");if(ample.delete()){System.out.println(ample.getName()+" is successfully deleted from your system"); } else{ System.out.println("An error is generated during the removal of this file"); } } }

Delete JSON File

We can also delete the .json file we created in the Java Create and Read File method in the same way:

Example: 

import java.io.File; // Import the File class from Java.io package public class Main{ public static void main(String[] args) {File ample=new File("NewFile.json");if(ample.delete()){System.out.println(ample.getName()+" is successfully deleted from your system"); } else{ System.out.println("An error is generated during the removal of this file"); } } }

 


Delete Folder

A folder can also be deleted. But there must be no content or files in it.

To delete a folder, don’t forget to create it first manually in the public directory (C → Users → Public )

A folder named Folder1 is delete in the example below:

Example: 

import java.io.File; public class Main { public static void main(String[] args) {File mrx=new File("C:\\Users\\public\\Folder1"); if(mrx.delete()){ System.out.println("The Folder: "+mrx.getName()+" is deleted from your system"); } else{ System.out.println("Such Directory does not Exist"); } } }

Similarly, We have also deleted the Folder2 from the directory:

Example: 

import java.io.File; public class Main { public static void main(String[] args) {File mrx=new File("C:\\Users\\public\\Folder2"); if(mrx.delete()){ System.out.println("The Folder: "+mrx.getName()+" is deleted from your system"); } else{ System.out.println("Such Directory does not Exist"); } } }

 

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *