I'm running Java on a Unix platform. How can I get a list of all mounted filesystems via the Java 1.6 API?
I've tried File.listRoots()
but that returns a single filesystem (that is, /
). If I use df -h
I see more than that:
Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
/dev/disk0s2 931Gi 843Gi 87Gi 91% 221142498 22838244 91% /
devfs 187Ki 187Ki 0Bi 100% 646 0 100% /dev
map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /home
/dev/disk1s2 1.8Ti 926Gi 937Gi 50% 242689949 245596503 50% /Volumes/MyBook
/dev/disk2 1.0Gi 125Mi 875Mi 13% 32014 223984 13% /Volumes/Google Earth
I would expect to see /home
as well (at a minimum).
In Java7+ you can use nio
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
public class ListMountedVolumesWithNio {
public static void main(String[] args) throws IOException {
for (FileStore store : FileSystems.getDefault().getFileStores()) {
long total = store.getTotalSpace() / 1024;
long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024;
long avail = store.getUsableSpace() / 1024;
System.out.format("%-20s %12d %12d %12d%n", store, total, used, avail);
}
}
}
Java doesn't provide any access to mount points. You have to run system command mount
via Runtime.exec()
and parse its output. Either that, or parse the contents of /etc/mtab
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With