简体   繁体   中英

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM