JAVA监控windows7系统的USB插拔事件

package usb;

import java.io.File;

public class UsbDevice {
//扫描系统的盘符
File[] root = File.listRoots();

public static void main(String[] args) {
new UsbDevice().searchUSB();
}

public void searchUSB() {
System.out.println("扫描系统...");
while (true) {
//循环里扫描盘符
File[] files = File.listRoots();
//比较长度
if (files.length > root.length) {
//重新赋值给系统root 相当于加1
root = File.listRoots();
System.out.println("插入");
} else if (files.length < root.length) {
//重新赋值给系统root 相当于减1
root = File.listRoots();
System.out.println("拔出");
}
}

}
}

猜你喜欢

转载自www.cnblogs.com/maocai2018/p/9938629.html