Trong bài này chúng ta sẽ học cách xóa file trong Java, thao tác xóa file trong Java rất đơn giản. Bất cứ việc gì cũng vậy, khởi tạo – xây dựng thì lâu nhưng phá hoại thì rất nhanh :3
Để xóa một file nào đó bằng ngôn ngữ Java thì bạn sử dụng phương thức Delete(), nó được tích hợp sẵn trong đối tượng File.
| 
 1 
 | 
public boolean delete() | 
Phương thức này sẽ trả về TRUE nếu xóa thành công, và false nếu xóa thất bại.
Xem ví dụ dưới đây.
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
 | 
import java.io.File;public class DeleteFileJavaDemo{   public static void main(String[] args)   {          try{         //Specify the file name and path         File file = new File("C:\\myfile.txt");         /*the delete() method returns true if the file is          * deleted successfully else it returns false          */         if(file.delete()){            System.out.println(file.getName() + " is deleted!");         }else{            System.out.println("Delete failed: File didn't delete");          }       }catch(Exception e){           System.out.println("Exception occurred");           e.printStackTrace();        }    }} | 
Giả file C:\\myfile.txt tồn tại thì sau khi chạy chương trình này ta sẽ thu được kết quả như sau:
myfile.txt is deleted! Theo:freetuts.net
                                       


							
							
							
							
							
							
							
							
							
							
							
							
							
															
							
															
							
															
							
                            
            
            