Package com.baikeyang.utils;
Import Java.io.File;
Import Java.util.LinkedHashMap;
Import Java.util.Map;
U disk Detection
public class Checku {
Storage disk Status
private static map<string, boolean> Map = new linkedhashmap<string, boolean> ();
Defining disks
private static final string[] arr = new string[] {"C", "D", "E", "F", "G", "H", "I", "J"};
public static void Main (string[] args) {
Init ();
Check ();
SYSTEM.OUT.PRINTLN ("USB drive Detected");
SYSTEM.OUT.PRINTLN (map);
}
Dead loop detects each disk status
public static void Check () {
File file;
for (;;) {
for (String Str:arr) {
File = new file (str + ": \");
If the disk now exists and does not exist before
Just plug in the USB stick and return
if (file.exists () &&!map.get (str)) {
Return
}
Update saved state every time a state change is required
Re-update status if the state you just detected is different from the original state
Must be placed below the IF statement above
if (file.exists () = Map.get (str)) {
Map.put (str, file.exists ());
}
}
try {
Thread.Sleep (1 * 1000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
Initialize disk state, existence true, otherwise false
public static void Init () {
File file;
for (String Str:arr) {
File = new file (str + ": \");
Map.put (str, file.exists ());
}
}
}
Java programs under Windows are used to detect USB drives