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.
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook