如何检查 java.nio.file.Path 是否来自默认文件系统?

How to check if java.nio.file.Path is from the default file system?

检查 java.nio.file.Path 是否来自默认文件系统的路径的最简单方法是什么?

我正在使用仍然使用 java.io.File 但也允许传递 java.net.URL.

的旧 API

我想出了两个可能的解决方案,但它们对我来说似乎不太干净:

  1. 在调用 Path.toFile() 时捕获 UnsupportedOperationException 但随后我会为正常的程序流程使用异常。

  2. 调用Path.toUri()并检查方案是"file"还是null

pathInstance.getFileSystem().equals(FileSystems.getDefault())