javamacosremovable-storage

Detecting removable drives in Mac


I am looking for a Java code whereby I can detect all volumes (or drives) in a Mac. I have seen various codes on the internet but nothing is working. The current code I am using is shown below:

FileSystemView fsv = FileSystemView.getFileSystemView();
File[] roots = fsv.getRoots();
for (File f: roots) {
    System.out.println(fsv.getSystemDisplayName(f);
}

This is not working on a Mac for me. Does someone know what code will allow me to detect drives on Mac?

Thanks a lot.


Solution

  • Mac OS is based on Unix.

    Drives are not mounted in the root folder(s) - which will typically be "/".

    They are usually mounted in /dev/.

    To ensure where your drives are mounted, open Terminal and type:

    diskutil list
    

    Use that to get your mounting points.

    For instance (ugly code!):

    FileSystemView fsv = FileSystemView.getFileSystemView();
    File dev = fsv.getChild(fsv.getRoots()[0], "dev");
    for (String listed: dev.list()) {
        System.out.println(listed);
    }