The Definition
The interface states the following about roots:
A root component, that identifies a file system hierarchy, may also be present.
So as you see, the comment seems to imply that roots are used for file system hierarchies.
Now we have to reason about what an absolute path is. The interface tells us the following:
An absolute path is complete in that it doesn't need to be combined with other path information in order to locate a file.
So, as you see, there is no word about roots in the definition about absolute paths. The only restriction is that we have to be able to locate the file without further information.
Hierarchical File Systems
Most file system are hierarchical, i.e., they are trees (or graphs if we consider links) or forests. The root in a tree is a node that is not the child of another node (excluding links). Windows file systems are, for example, forests, as they have many roots (C:
,D:
,...). Linux has usually only one root which is /
. Roots are very important as without them it would be hard to start locating a file. In such file systems, you can usually rely on each absolute path having a root.
Non-Hierarchical File Systems
As long as we have a hierarchical file system, we can anticipate a root in an absolute path, but what if we don't have one? Then, an absolute path might not contain a root.
An example that comes to my mind: Distributed file systems like Chord. These are often not hierarchical so the meaning of roots is usually undefined. Instead, a file hash identifies a file (SHA-1 in Chord). So a valid Chord path might look like this:
cf23df2207d99a74fbe169e3eba035e633b65d94
This is an absolute path. One can retrieve the associated file without further information, so the path is absolute. However, I see no root. We could define the whole hash to be its own root (then each file would be its own root), but nobody can guarantee that every person that implements a Chord file system will agree to this. So there might be reasonable implementations that do not treat these hashes as roots. In such a file system, each path would be absolute, but none would contain a root.
If I would implement a non-hierarchical file system, I would always return null
as root, as IMHO a root is not a defined concept in a non-hierarchical file system. Since I think like this, other devs might think so as well. Consequently, you may not assume that every absolute path has a root.
Note that distributed file systems are quite common in many areas, so this is not merely a corner case that will never be implemented. I think you have to anticipate it.
Conclusion
- The interface does not mandate that each absolute path must have a root
- There are reasonable file systems in which having no root makes sense
- An Oracle tutorial as mentioned in the comments is no contract for the interface. You should not rely on this
So there will be people implementing file systems without roots; you should anticipate this.
FileSystem
is apublic abstract class
. You may want to extend it (and thePath
instances it returns) for some reason to do that. – ToreyFileSystem
helper library and explore all the nooks and crannies of the API ;) – Kitsch