A Java library to get a list of all usb storage devices connected to the computer and has the capability of unmount them. It works on the three main operating systems (Windows, Linux and OS X).
To include this library in your project, add the following on your pom.xml
:
<project>
<dependencies>
<!-- New dependency -->
<dependency>
<groupId>net.samuelcampos</groupId>
<artifactId>usbdrivedetector</artifactId>
<version>2.2.1</version>
</dependency>
</dependencies>
</project>
USBDeviceDetectorManager driveDetector = new USBDeviceDetectorManager();
// Display all the USB storage devices currently connected
driveDetector.getRemovableDevices().forEach(System.out::println);
// Add an event listener to be notified when an USB storage device is connected or removed
driveDetector.addDriveListener(System.out::println);
// Unmount a device
driveDetector.unmountStorageDevice(driveDetector.getRemovableDevices().get(0));
Once you invoke addDriveListener
, your application keep running because it will internally create an ScheduledExecutorService
.
To finish your application, just invoke the close
method;
// Shutdown an initialized USBDeviceDetectorManager
driveDetector.close();