文件删除时可以选择File.delete()和Files.delete(Path path),这两个方法到底有什么区别呢?

//删除暂存的pdf
File file =new File(pdfFilename);
file.delete();

Path path2 = Paths.get(pdfFilename);
Files.delete(path2);

以下就用表格一一列举出来File.delete()和Files.delete(Path path)的区别:

 

File.delete()

Files.delete(Path path)

JDK

JDK1.0

JDK1.7

来源

java.io.File对象的实例方法

java.nio.file.Files类的静态方法

参数

无参

Java.nio.file.Path

返回值

boolean

void

异常声明

无声明

声明抛出java.io.IOException

文件不存在

不抛异常,返回false

抛java.nio.file.NoSuchFileException

删除非空目录

无法删除,返回false

无法删除,抛java.nio.file.DirectoryNotEmptyException

删除被占用文件

无法删除,返回false

无法删除,抛java.nio.file.FileSystemException

其他原因文件无法删除

不抛异常,返回false

抛java.io.IOException的具体子类

File.delete()和Files.delete(Path path)的区别的更多相关文章

  1. List or delete hidden files from command prompt(CMD)

    In Windows, files/folders have a special attribute called hidden attribute. By setting this attribut ...

  2. Find and delete duplicate files

    作用:查找指定目录(一个或多个)及子目录下的所有重复文件,分组列出,并可手动选择或自动随机删除多余重复文件,每组重复文件仅保留一份.(支持文件名有空格,例如:"file  name" ...

  3. 报错:Can't find a source file at "xxxxx“Locate the file or edit the source lookup path to include its location.

    调试问题: Can't find a source file at "/tmp/TI_MKLIB6sLCzz/SRC/exit.c" Locate the file or edit ...

  4. Flume启动报错[ERROR - org.apache.flume.sink.hdfs. Hit max consecutive under-replication rotations (30); will not continue rolling files under this path due to under-replication解决办法(图文详解)

    前期博客 Flume自定义拦截器(Interceptors)或自带拦截器时的一些经验技巧总结(图文详解)   问题详情 -- ::, (SinkRunner-PollingRunner-Default ...

  5. file的这几个取得path的方法各有不同,下边说说详细的区别

    html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...

  6. open_basedir restriction in effect. File() is not within the allowed path(s)

    目前发现eaccelerator安装之后如果php.ini中设置open_basedir将导致open_basedir的一些报错(open_basedir restriction in effect. ...

  7. More than one file was found with OS independent path 錯誤

    More than one file was found with OS independent path 'lib/armeabi/libmrpoid.so',. 翻譯過來就是:在操作系統的獨立目錄 ...

  8. hudson.AbortException: No files found in path D:\testproject\project2\testoutput\ with configured filemask: output.xml

    错误描述: hudson.AbortException: No files found in path D:\testproject\project2\testoutput\ with configu ...

  9. More than one file was found with OS independent path 'META-INF/LICENSE' | Error:Could not read \build\intermediates\typedefs.txt (系统找不到指定的文件。)

    FAQ1: Error:Could not read E:\new\PlatformLibrary\CommonLibrary\build\intermediates\typedefs.txt: E: ...

  10. android异常 More than one file was found with OS independent path 'META-INF/XXX'

    android 异常总结:一个文件在jar包中出现多次. Error:Execution failed for task ':app:transformResourcesWithMergeJavaRe ...

随机推荐

  1. [转帖]【软件测试】Jmeter性能测试(性能测试,Jmeter使用与结果分析)

    文章目录 前言 一.性能测试 1. 什么是性能测试? 2. 性能测试的重要性 3. 性能指标--QPS和TPS ①QPS ②TPS 二.压测工具Jmeter 1. 什么是Jmeter? 2. Jmet ...

  2. Linux 查询最近占用内存最多的十个进程的方法

    ps -eo rss,pid,user,command --sort -rss | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { ...

  3. VM PowerCli的简单安装和使用学习

    1. Win10 上面安装 下载 zip包并且进行安装 win10 已经带了powershell 安装比较简单, 只不过安装时会提示 powershell的权限有问题需要打开powershell 执行 ...

  4. CentOS7 RPM离线安装PG12的办法

    1. 先需要下载相应的rpm包 地址 https://pkgs.org/search/?q=postgresql12 一般至少要下载如下四个包 postgresql12-12.3-1PGDG.rhel ...

  5. js数组修改后会互相影响

    // 假设httpServe 是服务器返回来的数据 // 我们这里有一个需求, // 某一个区域需要对这一份数据进行展示 // 另一个区域需要只需要展示前1条数据 let httpServe = [ ...

  6. SpringCloud-Gateway搭建保姆级教程

    一.网关介绍 1.什么是网关? 使⽤服务⽹关作为接⼝服务的统⼀代理,前端通过⽹关完成服务的统⼀调⽤ 2.⽹关可以⼲什么? 路由:接⼝服务的统⼀代理,实现前端对接⼝服务的统⼀访问 过滤:对⽤户请求进⾏拦 ...

  7. 时间工具类-Java

    1.根据传入时间获取该时间所在季度的第一天 /** * 根据传入时间获取传入日期所在季度的第一天的日期 * * @param startDate * @return */ public static ...

  8. SpringBoot接入阿里云oss

    1.pom中添加阿里云oss坐标 <?xml version="1.0" encoding="utf-8"?> <dependencies&g ...

  9. 《Mastering ABP Framework》图书目录

    以下是<Mastering ABP Framework>的中文目录,目前个人正在进行翻译中,如果您对本书感兴趣,也想使用或者学习框架设计,可以访问该地址进行登记,本文只是一个目的的展示和购 ...

  10. Web入门:JavaScript文字动画

    欢迎来的我的小院,恭喜你今天又要涨知识了! 案例内容 利用JavaScript实现文字逐步展现的动画效果. 演示 学习 <!DOCTYPE html> <html lang=&quo ...