3

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

Jimmy T.
  • 4,033
  • 2
  • 22
  • 38

1 Answers1

4
pathInstance.getFileSystem().equals(FileSystems.getDefault())
the8472
  • 40,999
  • 5
  • 70
  • 122
  • I think, a reference comparison (`==`) is sufficient here, especially, as it would actually counteract the intention, if a file system had a contradicting `equals` implementation. – Holger Sep 26 '16 at 11:02