jeff-saupe / usbdrivedetector

A Java library to get a list of all usb storage devices connected to the computer.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

USB Drive Detector

Build Status | version

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

Maven dependency

To include this library in your project just use:

<dependency>
    <groupId>net.samuelcampos</groupId>
    <artifactId>usbdrivedetector</artifactId>
    <version>2.1.2</version>
</dependency>

Usage examples

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

About

A Java library to get a list of all usb storage devices connected to the computer.

License:MIT License


Languages

Language:Java 99.1%Language:Shell 0.9%