在 Java 中检测 USB 驱动器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3831825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:36:46  来源:igfitidea点击:

Detect USB Drive in Java

java

提问by bnovc

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

如何检测 USB 驱动器何时连接到 Windows、Linux 或 Mac 中的计算机?

The only way I have seen online to do this is to iterate the drives, but I don't think there is a very good way to do that cross-platform (e.g. File.listRoots() in Linux only returns "/"). Even in Windows this would cause problems reading from every device, such as a network drive that takes a long time to access.

我在网上看到的唯一方法是迭代驱动器,但我认为没有一个很好的跨平台方法(例如,Linux 中的 File.listRoots() 只返回“/”)。即使在 Windows 中,这也会导致从每个设备(例如需要很长时间访问的网络驱动器)读取的问题。

There is a library called jUsb that sounds like it accomplishes this in Linux, but it doesn't work in Windows. There is also an extension to this called jUsb for Windows, but that requires users to install a dll file and run a .reg. Neither of these seem to be developed for several years, so I'm hoping a better solution exists now. They're also overkill for what I need, when I only want to detect if a device is connected that contains a file I need.

有一个名为 jUsb 的库,听起来像是在 Linux 中实现了这一点,但它在 Windows 中不起作用。还有一个名为 jUsb for Windows 的扩展,但这需要用户安装 dll 文件并运行 .reg。这些似乎都没有开发好几年,所以我希望现在有更好的解决方案。当我只想检测是否连接了包含我需要的文件的设备时,它们对于我需要的东西也太过分了。

[Edit] Furthermore, jUsb apparently doesn't work with any recent version of Java, so this isn't even an option...

[编辑] 此外,jUsb 显然不适用于任何最新版本的 Java,所以这甚至不是一个选项......

Thanks

谢谢

回答by Favonius

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_DEVICECHANGEevent. Following links may help

上次我检查有没有用于 Java 和 Windows 的开源 USB 库。我使用的简单技巧是编写一个小型 JNI 应用程序来捕获WM_DEVICECHANGE事件。以下链接可能会有所帮助

  1. http://www.codeproject.com/KB/system/DriveDetector.aspx
  2. http://msdn.microsoft.com/en-us/library/aa363480(v=VS.85).aspx
  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/)

如果您不想弄乱 JNI,请使用带有 JNA 的 USB 的任何 Windows 本机库(https://github.com/twall/jna/

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

虽然我建议使用WM_DEVICECHANGE方法...因为您的要求只是一条通知消息....

回答by samuelcampos

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

我制作了一个小型库来检测 Java 上的 USB 存储设备。它适用于 Windows、OSX 和 Linux。看一看:https: //github.com/samuelcampos/usbdrivedetector

回答by Badreddine Leghrib

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

回答by Denis

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

我创建了适用于 Linux 和 Windows 的代码检查这个

 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*/
           }

      }
 }

回答by HyperNeutrino

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 orpulled out, use DriverChecker.driversChangedSinceLastUpdate()(returns boolean).

我写了这个程序。在程序开始时,执行DriverCheck.updateDriverInfo(). 然后,要检查 USB 是否已插入拔出,请使用DriverChecker.driversChangedSinceLastUpdate()(returns boolean)。

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

要检查是否已插入 USB,请使用newDriverDetected(). 要检查 USB 是否已被移除,请使用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.

这几乎会检查从 A:/ 到 Z:/ 的所有磁盘驱动器。其中一半甚至不存在,但无论如何我都会检查它们。

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

回答by BullyWiiPlaza

Check out thiscode. To fulfill your demands, simply pollto detect the USB drive and continue when you got it.

看看这个代码。为满足您的需求,只需轮询以检测 USB 驱动器,并在获得后继续。