I started using google jimfs and I can't understand how I can get file from path. In source code I see that Path.toFile throws UnsupportedOperationException. But how then can I use it without files? For example if my application need to know if some path is folder or file.
Asked
Active
Viewed 4,261 times
1 Answers
6
The JSR 203 API has all the tools you need for that; and in this case, the Files
class.
In spite of its name, it handles everything Path
. For instance, you can use:
Files.isDirectory(thePath)
to test whether a file is a directory. But there are also other ways to test for the same thing.

fge
- 119,121
- 33
- 254
- 329
-
Thank you! But why did they do like that? – Pavel_K Jul 02 '15 at 11:28
-
1Precisely because a `Path` can be issued from different filesystems. The `Path` class is "busy" enough as it is without adding more methods to it... – fge Jul 02 '15 at 11:29
-
Do I understand you right - because Path can be linked to different file systems and File can't? – Pavel_K Jul 02 '15 at 11:30
-
1Yes, exactly. A `File` can only be issued from what JSR 203 calls the default filesystem -- see `FileSystems.getDefault()`. In fact, when you do `Paths.get(whatever)`, this is exactly equivalent to `FileSystems.getDefault().getPath(whatever)`. – fge Jul 02 '15 at 11:33
-
Thank you for such detailed answer! – Pavel_K Jul 02 '15 at 11:35
-
2@JimJim2000: Yeah, as fge indicates, it's basically impossible to get a `File` object for something other than the default file system, so you can't have an in-memory file system like Jimfs and use `File`. That's part of the reason the new API (`Path`, `Files`, etc.) was created. – ColinD Jul 02 '15 at 14:36
-
I don't understand why someonee downvoted my question. Is my question stupid? – Pavel_K Jul 07 '15 at 04:25
-
@JimJim2000: Maybe because they thought you should have researched the API you were using more before asking a question about it? I don't know... I wouldn't worry about it. – ColinD Jul 07 '15 at 17:24