How to detect a usb drive in Java

…衆ロ難τιáo~ 提交于 2019-12-20 02:43:07

问题


I would like to know how to detect a usb drive. I have already read this post but I am interested in knowing if we can just use java.io.*; to check for the usb drive. The post that i have read up, does kind of explain how to use java.io.*; but I didn't quite understand. If someone could provide an example, that would be great. Thank you.


回答1:


You could you the File.listRoots() Method.

There is a Post about that: Post on Stackoverflow on File.listRoots()

The problem here is that you would need a name or some other info to identify the drive as a USB-Drive. Else you can't tell normal drives and usb-drives appart with this method.




回答2:


This works at least on Linux

File f = new File("/media");
for(File fls : f.listFiles()){
    System.out.print(fls+"\n");
}

Remember external plugged device storage on Linux can be found on /media



来源:https://stackoverflow.com/questions/6547762/how-to-detect-a-usb-drive-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!