Get FileStore object for any given path
java, linux, macos, os-agnostic
Solution
I found a reference I had previously missed on the last page of the NIO.2 document trail.
I wrote up some test code that gave me exactly what I needed:
public void getPathFilesystem(String path)
{
try
{
URI rootURI = new URI("file:///");
Path rootPath = Paths.get(rootURI);
Path dirPath = rootPath.resolve(path);
FileStore dirFileStore = Files.getFileStore(dirPath);
printFileStore(dirFileStore, path);
}
catch (IOException | URISyntaxException e)
{
e.printStackTrace();
}
}
public void printFileStore(FileStore filestore, String path)
{
try
{
System.out.println("Name: " + filestore.name());
System.out.println("\tPath: " + path);
System.out.println("\tSize: " + filestore.getTotalSpace());
System.out.println("\tUnallocated: " + filestore.getUnallocatedSpace());
System.out.println("\tUsable: " + filestore.getUsableSpace());
System.out.println("\tType: " + filestore.type());
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
Problem
Edit: I recognize that FileSystem.getDefault() will give me what I was looking for in my original question statement. I am attempting to use FileSystem.getFileSystem(URI) to get the FileSystem for any given path. I am attempting to develop some code that will give me a java.nio.file.FileSystem object for a given path. Here is some grossly simplified example code to give a better idea of what is being attempted: ``` public FileSystem getCwdFilesystem() { URI cwdUri = null; String delimiter = ""; try { _cwd = System.getProperty("user.dir"); cwdUri = new URI("file", delimiter + _cwd, null); } catch (URISyntaxException ue) { System.out.println("URI Creation failure on URI: " + _cwd); ue.printStackTrace(); System.exit(1); } System.out.println("Filestore data for CWD: " + cwdUri.toString()); return (FileSystems.getFileSystem(cwdUri)); } ``` Upon execution, an exception is throw on the last line of code: ``` Filestore data for CWD: file:/Users/redacted/Documents/Java%20Projects/ExampleCode Exception in thread "main" java.lang.IllegalArgumentException: Path component should be '/' at sun.nio.fs.UnixFileSystemProvider.checkUri(UnixFileSystemProvider.java:77) at sun.nio.fs.UnixFileSystemProvider.getFileSystem(UnixFileSystemProvider.java:92) at java.nio.file.FileSystems.getFileSystem(FileSystems.java:217) at examplecode.FilesystemCapacity.getCwdFilesystem(FilesystemCapacity.java:54) at examplecode.FilesystemCapacity.main(FilesystemCapacity.java:33) Java Result: 1 ``` When I make a small update to the delimiter variable: ``` String delimiter = "/"; ``` I get a different error message thrown from the same place: ``` Filestore data for CWD: file://Users/redacted/Documents/Java%20Projects/ExampleCode Exception in thread "main" java.lang.IllegalArgumentException: Authority component present at sun.nio.fs.UnixFileSystemProvider.checkUri(UnixFileSystemProvider.java:73) at sun.nio.fs.UnixFileSystemProvider.getFileSystem(UnixFileSystemProvider.java:92) at java.nio.file.FileSystems.getFileSystem(FileSystems.java:217) at examplecode.FilesystemCapacity.getCwdFilesystem(FilesystemCapacity.java:54) at examplecode.FilesystemCapacity.main(FilesystemCapacity.java:33) Java Result: 1 ``` Adding additional "/" characters to the delimiter simply gets me the first error message again. What am I doing wrong?