gpt4 book ai didi

java - 在 java 中检测外部硬盘和 sandisk cruzer blade 16 GB 可移动

转载 作者:行者123 更新时间:2023-11-29 08:47:27 25 4
gpt4 key购买 nike

我正在尝试打印驱动器盘符及其系统类型。在此程序中,它将硬盘驱动器打印为逻辑驱动器,将 cd 驱动器打印为 cd 驱动器,将存储卡打印为可移动驱动器。当我插入外部硬盘和 pendrive (sandisk cruzer blade 16 GB) 时,它显示为逻辑驱动器。我想检测并打印外部硬盘和 pendrive 作为“可移动设备”,因为它们是可移植的。请帮我怎么做。这是代码。

import javax.swing.filechooser.FileSystemView;
import java.io.File;
public static void main(String x[])
{
File paths[];
paths = File.listRoots();
for(File path: pahts)
{
System.out.println("Drive Name: "+path);
System.out.println("Description: "+fsv.getSystemTypeDescription(path));


}



}

最佳答案

您可以使用特定于操作系统的命令并通过 java Runtime 执行它。例如,在 Windows 中,您可以使用命令 wmic获取所有驱动器详细信息,如设备名称、标题、文件系统、类型等。例如,wmic logicaldisk get deviceid driveType 将列出所有可用的驱动器号及其类型。逻辑盘有7种驱动类型

0 = Unknown
1 = No Root Directory
2 = Removable Disk
3 = Local Disk
4 = Network Drive
5 = Compact Disc
6 = RAM Disk

如您所见,类型 2 就是您正在寻找的类型。除了 logicaldisk 详细信息外,wmic 还可用于检索系统的所有方面,如 cpu、bios 等。可以找到 wmic 中所有命令的列表 here .

同样你有udevadm在 Linux 中获取设备详细信息和 system_profiler 的命令在 Mac 中。因此,如果您想要一种独立于 plantform 的方式来访问详细信息,您可以根据当前操作系统更改命令。使用 System.getProperty("os.name") 识别当前操作系统。

Windows 操作系统的示例程序

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DriveDetails {

public static void main(String[] args) {
String command = "wmic logicaldisk where drivetype=$$ get deviceid";
for (DriveType driveType : DriveType.values()) {
String query = command.replace("$$", String.valueOf(driveType.getType()));
System.out.println(driveType.getDescription());
System.out.println(executeCommand(query).trim());
}
}

private static String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p = null;
BufferedReader reader = null;
try {
p = Runtime.getRuntime().exec(command);
reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
boolean flag = true;
while ((line = reader.readLine()) != null) {
if(flag) {
flag = false;
continue; // Skipping header
}
output.append(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (p != null)
p.destroy();
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return output.toString();
}

enum DriveType {
UNKNOWN(0, "Unknown"),
NO_ROOT_DIRECTORY(1, "No Root Directory"),
REMOVABLE_DISK(2, "Removable Disk"),
LOCAL_DISK(3, "Local Disk"),
NETWORK_DRIVE(4, "Network Drive"),
COMPACT_DISC(5, "Compact Disc"),
RAM_DISK(6, "RAM Disk");

private DriveType(int type, String description) {
this.type = type;
this.description = description;
}

private int type;
private String description;

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
}

关于java - 在 java 中检测外部硬盘和 sandisk cruzer blade 16 GB 可移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24402964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com