Brian Heilig
Brian Heilig

Reputation: 648

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.

Upvotes: 2

Views: 401

Answers (2)

CormacS
CormacS

Reputation: 26

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 :)

Upvotes: 1

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

As far as I know, no - Java does not provide access to the information that you're looking for.

I think that your only real option is to write a shared library, and use either JNI or JNA to access it.

Upvotes: 1

Related Questions