Java Delete File In Hindi


अभी तक आपने file create / write और file read के बारे में पढ़ा और समझा , इस topic में आप कि कैसे किसी file / folder को delete करते हैं।

Java Delete File

Java में किसी file को delete करने के लिए File class का delete() method use किया जाता है।

Java Delete File Example

File : DeleteFile.java

CopyFullscreenClose FullscreenRun
// import File class from java.io package.
import java.io.File; 
public class DeleteFile { 
  public static void main(String[] args) {
    File f = new File("testfile.txt");
    // check if exists.
    if (f.exists()) { 
      if (myObj.delete()) { 
        System.out.println("File deleted successfully.");
      } else {
        System.out.println("Failed to delete the file.");
      } 
    } 
    else {
      System.out.println("The file does not exist.");
    }
  }
}
Output
File deleted successfully.

Java Delete Directory

delete() method का use करके आप किसी folder कको भी delete कर सकते हैं , लेकिन ध्यान रहे कि वो folder empty होना चाहिए।

Java Delete Direactory Example

File : DeleteFolder.java

CopyFullscreenClose FullscreenRun
// import File class from java.io package.
import java.io.File; 
public class DeleteFolder { 
  public static void main(String[] args) {
    File f = new File("C:\\Users\\verma\Desktop\\myfolder");
    if (f.delete()) { 
      System.out.println("Folder deleted successfully.");
    } else {
      System.out.println("Failed to delete the file.");
    } 
  }
}
Output
Folder deleted successfully.

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook