java File中File.exists() 与 File.isFile()区别?

来源:互联网 发布:3d 打印 切片算法思想 编辑:程序博客网 时间:2024/05/16 08:31

这种区别一般在Linux环境中比较明显。

比如在Linux环境中/dev/null这个文件是个特殊文件。

使用File.isFile()方法返回结果是false

使用File.exists()方法返回结果是true

也就是说如果是我们常见的普通文件,两者是没多少区别的!

区别只在于特殊文件中。

Use File.exists() when dealing with special files like named pipes, sockets or device files.Those are not regular files nor directories nor symlinks so both File.isFile() and File.isDirectory() will return false while File.exists() will return true. For example /dev/null (on Unix compatible OSes) is a device file.Theoretically there may be performance differences visible when processing large amounts of files. This depends also on filesystem, JVM implementation details, OS etc.Eg. on Android File.exists() is implemented using access() system call while File.isFile()/File.isDirectory() use stat(). In this case processing stat() output requires more logic in userspace than access().

http://stackoverflow.com/a/38620520/6952713

0 0