Làm thể để để xóa một tập tin bằng Java? (How to delete file in Java?)







Tiếp theo trong loạt bài Java IO, tôi sẽ trình bày cách để xóa một tập tin bằng cách sử dụng lớp File(class file) trong ngôn ngữ lập trinh Java.







Phương thức "delete()" được cung cấp trong lớp File(class file) của Java được sử dụng để xóa một tập tin được chỉ định. Đầu tiên bạn cần chuẩn bị một tập tin để thực hiện bài tập thực tập của bài viết này.



JavaDeleteFile.java
/**
 * @(#)JavaDeleteFile.java
 *
 * JavaDeleteFile application
 *
 * @author developer.bnson@gmail.com
 * @version 1.00 2014/3/10
 */

import java.io.File;

public class JavaDeleteFile {
   
    public static void main(String[] args) {
       
        try{

            File file = new File("D:\\Z-Test\\Demo\\VNLIVES.NET_CHIBI_001.jpg");

            if( file.delete()) {
                System.out.println(file.getName() + " is deleted!");
               
            } else {
                System.out.println("Delete operation is failed.");
            }

        } catch(Exception e) {

            e.printStackTrace();

        }
       
    }
}

Kết quả sau khi tập tin được xóa thành công sẽ là:

VNLIVES.NET_CHIBI_001.jpg is deleted!

Bào truy cập vào folder chứ tập tin xóa để  kiểm tra kết quả.





























No comments:

Post a Comment