我必须找出具有目录路径的对象的类型(文件夹/文件)。
例如我必须找出具有此路径的对象是一个文件。
/home/user/test.docx
我找到了一个可行的解决方案,但实际上并不是一个很好的解决方案。
try {
final Folder parentFolder = (Folder) session.getObjectByPath(path);
//throws exception when path points to a file
//do sth when it's a folder
} catch (final Exception e) {
//do sth when it's a document/file
}
我不能在这里使用'instance of',因为我无法在不知道输出类型的情况下获取对象(使用session.getObjectByPath)。
是否有更好的方法来找出带有其路径的对象类型?
而不是直接将其强制转换为Folder,您可以像这样检查instanceof: