java 检测目录下的文件

java.io.File class contains two methods using which we can find out if the file is a directory or a regular file in java.

java.io.File类包含两个方法,通过它们我们可以确定文件是java中的目录还是常规文件。

  1. isFile(): This method returns true if file exists and is a regular file, note that if file doesn’t exist then it returns false.isFile() :如果文件存在并且是常规文件,则此方法返回true,请注意,如果文件不存在,则返回false。
  2. isDirectory(): This method returns true if file is actually a directory, if path doesn’t exist then it returns false.isDirectory() :如果文件实际上是目录,则此方法返回true;如果路径不存在,则返回false。

While checking if a file is a directory or regular file, we should first check if the file exists or not. If it exists then only we should check if it’s a directory or file.

在检查文件是目录文件还是常规文件时,我们应该首先检查文件是否存在。 如果存在,那么只有我们应该检查它是目录还是文件。

A simple java program showing it’s usage.

一个显示其用法的简单Java程序。

package com.journaldev.files;import java.io.File;public class CheckDirectoryOrFile {public static void main(String[] args) {File file = new File("/Users/pankaj/source.txt");File dir = new File("/Users/pankaj");File notExists = new File("/Users/pankaj/notafile");System.out.println("/Users/pankaj/source.txt is file?"+file.isFile());System.out.println("/Users/pankaj/source.txt is directory?"+file.isDirectory());System.out.println("/Users/pankaj is file?"+dir.isFile());System.out.println("/Users/pankaj is directory?"+dir.isDirectory());System.out.println("/Users/pankaj/notafile is file?"+notExists.isFile());System.out.println("/Users/pankaj/notafile is directory?"+notExists.isDirectory());}}

Output of the above program is:

上面程序的输出是:

/Users/pankaj/source.txt is file?true
/Users/pankaj/source.txt is directory?false
/Users/pankaj is file?false
/Users/pankaj is directory?true
/Users/pankaj/notafile is file?false
/Users/pankaj/notafile is directory?false

翻译自: https://www.journaldev.com/909/check-file-directory-file-java

java 检测目录下的文件

java 检测目录下的文件_如何在Java中检查文件是目录还是文件相关推荐

  1. python怎样打开加密的文件_如何在Python中解密OpenSSL AES加密的文件?

    拉莫斯之舞 我将通过一些更正重新发布您的代码(我不想掩盖您的版本).当您的代码正常工作时,它不会检测到填充周围的一些错误.特别是,如果提供的解密密钥不正确,则填充逻辑可能会做一些奇怪的事情.如果您同意 ...

  2. matlab分析xml文件_如何在Java中读取XML文件(DOM分析器)

    matlab分析xml文件 Today we will learn how to read the XML file in Java. We will also learn how to parse ...

  3. sftp shell 批量上传文件_如何在shell脚本里使用sftp批量传送文件-阿里云开发者社区...

    主要步骤如下: 1.为运行shell脚本的本地用户生成密钥对 2.将其中的公钥分发到sftp欲登录的远程服务器上 3.编写并以上面的本地用户运行shell脚本 一.生成密钥对 在shell脚本中使用s ...

  4. scala 当前日期_如何在Scala中检查当前日期和时间?

    scala 当前日期 Scala is a general-purpose programming language, which is majorly used for data manipulat ...

  5. java将输出结果写入csv文件_如何在Java中将数据写入.csv文件?

    名为OpenCSV的库提供API来从.CSV文件读取数据或将数据写入.CSV文件.此处说明了如何使用Java程序写入.csv文件的内容. Maven依赖 com.opencsv opencsv 4.4 ...

  6. macos 虚拟镜像文件_如何在macOS中使用虚拟文件测试网络或硬盘速度

    macos 虚拟镜像文件 File transfer speeds can vary greatly from device to device. The same holds true for ne ...

  7. java如何打开word文档_如何在Java中打开和操作Word文档/模板?

    我知道自从我发布这个问题以来已经很长时间了,我说我会在完成后发布我的解决方案. 所以在这里. 我希望有一天它会帮助某人. 这是一个完整的工作类,您只需将它放在应用程序中,并将TEMPLATE_DIRE ...

  8. cam350 不能打开光绘文件_如何在CAM350中导入Allegro光绘

    内容简介: 本人之前遇到过 CAM350 导入 Allegro 格式光绘后钻孔偏移.异型孔不显示的问题,捣鼓过之后找到一个方法,在 Allegro 群友的建议下制作该文档进行适当的说明. 1. 导入 ...

  9. python 读取日志文件_如何在Python中跟踪日志文件?

    使用SH模块(PIP安装sh):from sh import tail# runs foreverfor line in tail("-f", "/var/log/som ...

最新文章

  1. sihpostreboot 权限_记一次使用post请求重启TP-LINK TL-WDR7661路由
  2. Python基于socket实现的TCP客户端
  3. python 利用 for ... else 跳出双层嵌套循环
  4. Expected an Objective directive after @ 的解决办法
  5. [Qt] 利用QtWebKit完成JavaScript访问C++对象
  6. cd返回上一 git_使用Git实现自动化部署项目
  7. Nginx 中 nginx.conf 详解
  8. GB28181的协议详解
  9. 华为路由器配置OSPF实现不同网段通信
  10. 从产品模式到生活方式,苏宁小Biu车联网迈过了哪些坎?
  11. Flink Watermark机制
  12. 电脑连接android手机测试,Android系统手机通过USB连接电脑上网
  13. CCNA(思科网络攻城狮) 滴水之力05
  14. 真无线蓝牙耳机哪个好用?学生党蓝牙耳机性价比排行榜
  15. split和ubound函数的用法
  16. SQL注入攻击及防御 手动注入+sqlmap自动化注入实战(网络安全学习12)
  17. day5 安装Linux服务器面板管理工具
  18. PHOTOSHOP CS打造素描MM
  19. SAP migo增强
  20. Vue 打包后,使用火狐与谷歌可以打开index,但是IE小贱人打开空白的解决方法

热门文章

  1. linux下搭建python机器学习环境
  2. Java==与equals方法的区别
  3. Backbone入门教程
  4. Swift实战-豆瓣电台(八)播放进度与时间
  5. 技术专题之-技术概述的目录
  6. Visual Studio 单元测试之二---顺序单元测试
  7. lucene分析(未完成)
  8. spring 连数据库的配置文件
  9. [转载] python---python中时间的应用(time模块)
  10. E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/t