javafilesystems

Determine filesystem type of given file in Java


Is there something similar to statfs in Java to determine the filesystem type of a given file? More specifically I'd like to determine if a file provided by a user is on a shared or local filesystem. I'm going to use this information to determine if the application is opening several identically named files on the local filesystem of a distributed application, or one shared file.

I suppose if I can get something like "NFS", "Lustre", "EXT3", etc either as a string or an enumeration then I can program a mapping to shared/not shared. Let's assume the file exists and is readable.


Solution

  • Here's how to do it with FileStore

    String format = getFormat(Path.of(file.getPath()));
    
    String getFormat (Path path) {
        return Files.getFileStore(path).type();
    }
    

    EDIT 22/7/24: This is implementation specific, in particular:

    It may indicate, for example, the format used or if the file store is local or remote.

    At least on MacOS, it was an adequate solution to see if files were on a FAT based filesystem or not. But your mileage may vary :)