java

How to delete a file in Java

In this tutorial, we are going to see how to delete a file in Java. Java provides methods to delete files. Unlike normal deletion operations in any operating system, files deleted using the java program are permanently deleted without being placed in the recycle bin.
 

How to delete a file in Java
import java.io.*; 
  
public class Main 
{ 
    public static void main(String[] args) 
    { 
        File myFile = new File("C:\\Users\\PC\\Desktop\\test.txt"); 
          
        if(myFile.delete()) 
        { 
            System.out.println("File deleted successfully"); 
        } 
        else
        { 
            System.out.println("Unable to delete the file"); 
        } 
    } 
}

Output:

File deleted successfully
[st_adsense] mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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