Detect USB Drive in Java

前端 未结 6 493
故里飘歌
故里飘歌 2020-12-01 06:35

How can I detect when a USB drive is attached to a computer in Windows, Linux, or Mac?

The only way I have seen online to do this is to iterate the drives, but I don

相关标签:
6条回答
  • 2020-12-01 07:09
    public class AutoDetect {
    
    static File[] oldListRoot = File.listRoots();
    public static void main(String[] args) {
        AutoDetect.waitForNotifying();
    
    }
    
    public static void waitForNotifying() {
        Thread t = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (File.listRoots().length > oldListRoot.length) {
                        System.out.println("new drive detected");
                        oldListRoot = File.listRoots();
                        System.out.println("drive"+oldListRoot[oldListRoot.length-1]+" detected");
    
                    } else if (File.listRoots().length < oldListRoot.length) {
        System.out.println(oldListRoot[oldListRoot.length-1]+" drive removed");
    
                        oldListRoot = File.listRoots();
    
                    }
    
                }
            }
        });
        t.start();
    }
    }
    
    0 讨论(0)
  • 2020-12-01 07:14

    Check out this code. To fulfill your demands, simply poll to detect the USB drive and continue when you got it.

    0 讨论(0)
  • 2020-12-01 07:15

    Last time I checked there were no open source USB library for java and in windows. The simple hack that I used was to write a small JNI app for capturing WM_DEVICECHANGE event. Following links may help

    1. http://www.codeproject.com/KB/system/DriveDetector.aspx
    2. http://msdn.microsoft.com/en-us/library/aa363480(v=VS.85).aspx

    In case you don't want to mess with the JNI then use any windows native library for USB with JNA ( https://github.com/twall/jna/ )

    altough i would suggest using WM_DEVICECHANGE approach... because your requirement is just a notification message....

    0 讨论(0)
  • 2020-12-01 07:20

    I created the code that works on Linux and windows check this

     import java.io.BufferedReader; 
     import java.io.File;
     import java.io.IOException;
     import java.io.InputStreamReader;
    
     public class Main{
     public static void main(String[] args) throws IOException{//main class
         Main m = new Main();//main method
         String os = System.getProperty("os.name").toLowerCase();//get Os name
         if(os.indexOf("win") > 0){//checking if os is *nix or windows
             //This is windows
             m.ListFiles(new File("/storage"));//do some staf for windows i am not so sure about '/storage' in windows
             //external drive will be found on 
         }else{
             //Some *nix OS
             //all *nix Os here
             m.ListFiles(new File("/media"));//if linux removable drive found on media
             //this is for Linux
    
         }
    
    
     }
    
     void ListFiles(File fls)//this is list drives methods
                 throws IOException {
         while(true){//while loop
    
    
     try {
        Thread.sleep(5000);//repeate a task every 5 minutes
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
         Process p = Runtime.getRuntime().exec("ls "+fls);//executing command to get the output
         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));//getting the output
                 String line;//output line
                    while((line = br.readLine()) != null){//reading the output
                     System.out.print("removable drives : "+line+"\n");//printing the output
               }
              /*You can modify the code as you wish. 
              * To check if external storage drivers pluged in or removed, compare the lenght
              * Change the time interval if you wish*/
               }
    
          }
     }
    
    0 讨论(0)
  • 2020-12-01 07:23

    I've made a small library to detect USB storage devices on Java. It works on Windows, OSX and Linux. Take a look at: https://github.com/samuelcampos/usbdrivedetector

    0 讨论(0)
  • 2020-12-01 07:24

    I wrote this program. At the beginning of the program, do DriverCheck.updateDriverInfo(). Then, to check to see if a usb has been plugged in or pulled out, use DriverChecker.driversChangedSinceLastUpdate() (returns boolean).

    To check if a usb has been inserted, use newDriverDetected(). To check if a usb has been removed, use driverRemoved()

    This pretty much checks for all disc drives from A:/ to Z:/. Half of them can't even exist, but I check for all of them anyways.

    package security;
    
    import java.io.File;
    
    public final class DriverChecker {
        private static boolean[] drivers = new boolean[26];
    
        private DriverChecker() {
    
        }
    
        public static boolean checkForDrive(String dir) {
            return new File(dir).exists();
        }
    
        public static void updateDriverInfo() {
            for (int i = 0; i < 26; i++) {
                drivers[i] = checkForDrive((char) (i + 'A') + ":/");
            }
        }
    
        public static boolean newDriverDetected() {
            for (int i = 0; i < 26; i++) {
                if (!drivers[i] && checkForDrive((char) (i + 'A') + ":/")) {
                    return true;
                }
            }
            return false;
        }
    
        public static boolean driverRemoved() {
            for (int i = 0; i < 26; i++) {
                if (drivers[i] && !checkForDrive((char) (i + 'A') + ":/")) {
                    return true;
                }
            }
            return false;
        }
    
        public static boolean driversChangedSinceLastUpdate() {
            for (int i = 0; i < 26; i++) {
                if (drivers[i] != checkForDrive((char) (i + 'A') + ":/")) {
                    return true;
                }
            }
            return false;
        }
    
        public static void printInfo() {
            for (int i = 0; i < 26; i++) {
                System.out.println("Driver " + (char) (i + 'A') + ":/ "
                        + (drivers[i] ? "exists" : "does not exist"));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题