java代码获取U盘名称等信息
时间: 2023-11-22 21:54:12 浏览: 32
你可以使用Java中的FileSystemView类来获取U盘名称等信息。以下是一个示例代码:
```java
import java.io.File;
import javax.swing.filechooser.FileSystemView;
public class USBInfo {
public static void main(String[] args) {
File[] roots = File.listRoots();
FileSystemView fsv = FileSystemView.getFileSystemView();
for (File root : roots) {
if (fsv.isDrive(root)) {
String name = fsv.getSystemDisplayName(root);
String type = fsv.getSystemTypeDescription(root);
long freeSpace = root.getFreeSpace();
long totalSpace = root.getTotalSpace();
long usedSpace = totalSpace - freeSpace;
System.out.println("Drive: " + root.getAbsolutePath());
System.out.println("Name: " + name);
System.out.println("Type: " + type);
System.out.println("Free space: " + freeSpace);
System.out.println("Total space: " + totalSpace);
System.out.println("Used space: " + usedSpace);
System.out.println();
}
}
}
}
```
该代码将列出计算机上所有的驱动器,并输出每个驱动器的名称、类型、可用空间和总空间。如果驱动器是U盘,则还会输出U盘的名称。