Java fayllarni o‘chirish
Faylni o‘chirish
Java tilida faylni o‘chirish uchun delete() metodidan foydalaning:
Misol
import java.io.File; // Import the File class
public class DeleteFile {
public static void main(String[] args) {
File myObj = new File("filename.txt");
if (myObj.delete()) {
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}
Natija quyidagicha bo‘ladi:
Deleted the file: filename.txt
Papkani o‘chirish
Papkani ham o‘chirishingiz mumkin. Biroq u bo‘sh bo‘lishi kerak:
Misol
import java.io.File;
public class DeleteFolder {
public static void main(String[] args) {
File myObj = new File("C:\\Users\\MyName\\Test");
if (myObj.delete()) {
System.out.println("Deleted the folder: " + myObj.getName());
} else {
System.out.println("Failed to delete the folder.");
}
}
}
Natija quyidagicha bo‘ladi:
Deleted the folder: Test
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
