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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541947.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Unity3D Adam Demo的学习与研究

1.简述 这篇文章是对Adam各种相关资料了解后进行一些精简的内容。如果你想仔细研究某个技术请跳转至unity相关页面。 Adam官方页面: https://unity3d.com/cn/pages/adam 搬运视频以及资源包网盘下载: http://pan.baidu.com/s/1jH6NF86 Adam这个demo由8个人的团队耗时6个月(part…

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

File类boolean isFile() (File Class boolean isFile()) This method is available in package java.io.File.isFile(). 软件包java.io.File.isFile()中提供了此方法。 This method is used to check whether the file is specified by filepath is a file or not. 此方法用于检…

要加油!

现实中我容易佩服一个人。 一个顽强的女人,一个艰苦奋斗的男人..... 但是在网络的世界里,我没有佩服过几个,但是不得不说的就是冰河。同样的年龄人家做的事情和我们做的事情差距是多么的大,真的想想心里都是天壤之别。 比一比才知…

Java DataOutputStream writeInt()方法及示例

DataOutputStream类writeInt()方法 (DataOutputStream Class writeInt() method) writeInt() method is available in java.io package. writeInt()方法在java.io包中可用。 writeInt() method is used to write the given integer value to the basic DataOutputStream as 4 b…

python安卓自动化实现方法_uiautomator +python 实现安卓UI自动化

简单实例注:安卓6.0以上的手机不会自动安装app-uiautomator.apk和app-uiautomator-test.apk,需要手动安装,否则报错ioerror RPC server not starteduiautomator pythonHTMLTestRunner 安卓UI自动化实现#coding:utf-8from uiautomator importD…

ES6特性之:Spread操作符

Spread操作符(...),也称作展开操作符,作用是将可迭代的(Iterable)对象进行展开。 比如有2个数组,我们要将其中一个数组中所有元素插入到另一个数组中,通过Spread操作符,就可以这样进行: var fruits ["…

Java类class isMemberClass()方法及示例

类的类isMemberClass()方法 (Class class isMemberClass() method) isMemberClass() method is available in java.lang package. isMemberClass()方法在java.lang包中可用。 isMemberClass() method is used to check whether the underlying class is a member class or not.…

velocity自定义函数_velocity基本语法和总结

一:基本语法:1、#set(#a "a")$a ##输出语句时直接写变量的名称即可2、判断语句:#if($a "a") ##判断语句没有括号,也是直接输出$a3、数组:#set($arry [0..10])$foreach($i in $arry)$i ##换行#e…

docker-machine指定cpu个数

序 给本机的一个服务压测,结果半天qps上不了万,而且经常跑满cpu,搞半天发现,docker里头才1核1G内存。原来boot2docker默认给docker-machine分配1个cpu和1G内存。 修改配置 docker-machine create \--driver virtualbox \--virtual…

Java ClassLoader findResources()方法与示例

ClassLoader类findResources()方法 (ClassLoader Class findResources() method) findResources() method is available in java.lang package. findResources()方法在java.lang包中可用。 findResources() method is used to find all the resources with the given resource …

Java ByteArrayInputStream mark()方法与示例

ByteArrayInputStream类mark()方法 (ByteArrayInputStream Class mark() method) mark() method is available in java.util package. mark()方法在java.util包中可用。 mark() method is used to set the current mark position in the stream from where read or write can b…

java mediainfo.dll_MediaInfo库的简单使用

想到一个问题, 如何获得一个图像文件(比如jpg, bmp, png)的信息. 自己查查文件的格式, 写一个解析, 应该不困难; 但是找了下现成的, 发现MediaInfo库已经可以非常好的实现需要的功能了.MediaInfo可以在sourceforge上找到, 是一个解析视频,音频, 图片等媒体文件的库. 可以得到文…

Redis配置和常用命令

1 redis.conf配置文件:2 引用3 #是否作为守护进程运行4 daemonize yes5 #配置pid的存放路径及文件名,默认为当前路径下6 pidfile redis.pid7 #Redis默认监听端口8 port 63799 #客户端闲置多少秒后,断开连接 10 timeout 300 11 #日志显示级别 …

oracle中dbms_DBMS中的功能依赖性和属性关闭

oracle中dbms功能依赖 (Functional Dependency) A relational Database management System (RDBMS) represents the database o a collection of relations/tables. A functional dependency is a constraint between two sets of attributes in a relation. It is the propert…

java invoke 泛型_利用Java反射机制和泛型,全自动解析json

有啦这个简直,太爽啦,利用Java 反射机制,利用Class 就可以得到 类的 变量 Field[] fieldscls.getDeclaredFields();还可以通过类中 的方法名字 去执行这个方法m1 cls.getDeclaredMethod(getMothodName(fields[j].getName()), String.class)…

2_C语言中的数据类型 (四)整数与无符号数

1.1 sizeof关键字 sizeof是c语言关键字,功能是求指定数据类型在内存中的大小,单位:字节 sizeof与size_t类型 1.1 int类型 1.1.1 int常量,变量 int就是32位的一个二进制整数,在内存当中占据4个字节…

python 示例_Python TextCalendar类别| pryear()方法与示例

python 示例Python TextCalendar.pryear()方法 (Python TextCalendar.pryear() Method) pryear() method is an inbuilt method of the TextCalendar class of calendar module in Python. It works on text calendars. It uses an instance of TextCalendar class and prints …

Spring实战——通过Java代码装配bean

上篇说的是无需半行xml配置完成bean的自动化注入。这篇仍然不要任何xml配置,通过Java代码也能达到同样的效果。 这么说,是要把上篇的料拿出来再煮一遍? 当然不是,上篇我们几乎都在用注解的方式如ComponentScan Component等就完成了…

java 谓词_java8-谓词(predicate)

传递代码我们首先看一个例子,假设你有一个 Apple 类,它有一个getColor方法,还有一个变量inventory保存着一个Apples的列表。你可能想要选出所有的绿苹果,并返回一个列表。通常我们用筛选(filter)一词来表达这个概念。在 Java 8之前…