Search code examples
javafilejava-8nio2

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


What's the easiest way to check if a java.nio.file.Path is a path from the default file system?

I'm working with an older API that still uses java.io.File but also allows to pass an java.net.URL.

I've come up with two possible solutions but they don't seem clean to me:

  1. Catching UnsupportedOperationException when calling Path.toFile() but then I would use exceptions for the normal program flow.

  2. Call Path.toUri() and check if the scheme is "file" or null


Solution

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