If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
अभी तक आपने file create / write और file read के बारे में पढ़ा और समझा , इस topic में आप कि कैसे किसी file / folder को delete करते हैं।
Java में किसी file को delete करने के लिए File class का delete() method use किया जाता है।
File : DeleteFile.java
// 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.");
}
}
}
File deleted successfully.
delete() method का use करके आप किसी folder कको भी delete कर सकते हैं , लेकिन ध्यान रहे कि वो folder empty होना चाहिए।
File : DeleteFolder.java
// 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.");
}
}
}
Folder deleted successfully.