myFile.isFile() 或 myFile.isDirectory() 是否有可能的第三种类型的功能?

myFile.isFile() or myFile.isDirectory() is there any third type of function possible?

我正在编写一个通用函数来检查变量是否包含文件或目录。所以我想出了一个想法,使用布尔 return 类型的函数来检查它。像这样:

function boolean checkFileOrDirectory(File myFile){
    // Assume file already exist
    if(myFile.isDirectory()){
        //myFile is a directory
        return true;
    } else {
        //myFile is a file
        return false;
    }
}

我想知道这个函数的准确性。如果有人能告诉我是否有任何类型的文件类型不属于 isFile() 或 isDirectory() ,我将不胜感激?

"file" 可能类似于符号链接。文件和文件的符号链接之间的区别是否重要取决于您的应用程序。


用布尔值表示"is something or something else"是个坏主意,因为不清楚"true"或"false"是什么意思。

即使您记得 "true" 表示 "is a directory","false" 在逻辑上表示 "is not a directory"... 所以 reader 仍然想知道 "well, I know it's not a directory, but what is it?"

你问这个问题的事实证明了这一点。

考虑改用枚举:

enum Kind { DIRECTORY, FILE }

这不仅是自我记录的(您将有类似 "if this is a file/else if this is a directory" 的代码),它还为将来添加其他类型留下了可能性。