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

这种区别一般在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

你可能感兴趣的:(Java)