博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java File类boolean delete()方法(带示例)
阅读量:2527 次
发布时间:2019-05-11

本文共 2735 字,大约阅读时间需要 9 分钟。

文件类布尔型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 methodsimport java.io.File;// import the Exception class because it may raise an exception // when working with filesimport 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.javaD:\Programs>java DeleteFileFile deleted Successfully C:\Users\computer clinic\OneDrive\Articles\javafiles.txtFile failure of deletion C:\Users\computer clinic\OneDrive\Articles\myjav.txt

翻译自:

转载地址:http://kbtzd.baihongyu.com/

你可能感兴趣的文章
阿里负载均衡,配置中间证书问题(在starcom申请免费DV ssl)
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
UVa 1658,Admiral (拆点+限制最小费用流)
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
音视频处理
查看>>
Careercup - Facebook面试题 - 5890898499993600
查看>>
浮点数在内存中的存储方式
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
STUCTS LABLE ‘S BENEFIT
查看>>
Vue 计算属性
查看>>
第十八次ScrumMeeting博客
查看>>
洛谷P2580 于是他错误的点名开始了 题解
查看>>