Java File类boolean delete()方法(带示例)

文件类布尔型delete() (File Class boolean delete())

  • This method is available in package java.io.File.delete().

    软件包java.io.File.delete()中提供了此方法。

  • This method is used to delete file or directory by using delete() method and this method is accessible with the File object.

    此方法用于通过使用delete()方法删除文件或目录,并且可以通过File对象访问此方法。

  • The return type of this method is Boolean i.e. it returns true or false if true that means file is successfully deleted and returns false that means the file is failed to delete.

    此方法的返回类型为Boolean,即返回true或false,如果为true表示文件已成功删除,则返回false表示文件删除失败。

Syntax:

句法:

    boolean delete(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is Boolean i.e. it returns true or false if true that means file is successfully created else returns false that means the file is failed to delete.

此方法的返回类型为Boolean,即返回true或false,如果为true则表示文件已成功创建,否则返回false表示文件无法删除。

Java程序演示delete()方法的示例 (Java program to demonstrate example of delete() method)

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an exception 
// when working with files
import java.lang.Exception;

public class DeleteFile {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows 
            // otherwise it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\javafiles.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjav.txt");

            // By using delete() method returns true that's means 
            // file is deleted successfully.
            if (file1.delete())
                System.out.println("File deleted Successfully" + " " + file1.getName());
            else
                System.out.println("File failure of deletion " + file1.getName());

            // By using delete() method returns false that's means 
            // file failure of deletion.
            if (file2.delete())
                System.out.println("File deleted Successfully" + file2.getName());
            else
                System.out.println("File failure of deletion" + " " + file2.getName());

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac DeleteFile.java

D:\Programs>java DeleteFile
File deleted Successfully C:\Users\computer clinic\OneDrive\Articles\javafiles.txt
File failure of deletion C:\Users\computer clinic\OneDrive\Articles\myjav.txt


翻译自: https://www.includehelp.com/java/file-class-boolean-delete-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值