You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java(JavaFX)获取Windows存储驱动器列表及FileSystemView使用问题

解决Windows下Java无法使用FileSystemView获取驱动器列表的问题

方案1:修复模块化项目的模块依赖

如果你的项目是Java 9+的模块化项目,FileSystemView属于java.desktop模块,默认不会自动引入。需要在项目的module-info.java中添加以下声明:

module your.module.name {
    requires java.desktop;
}

添加后重新编译项目,就能正常导入javax.swing.filechooser.FileSystemView,使用你找到的原代码。

方案2:调用Windows原生命令获取详细信息

如果不想处理模块依赖,可以直接调用Windows系统的wmic命令获取驱动器的完整信息,再在Java中解析输出:

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

public class DriveLister {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("wmic logicaldisk get caption,description,drivetype");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            
            String line;
            // 跳过表头行
            reader.readLine();
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty()) continue;
                
                // 拆分wmic的空格分隔输出
                String[] segments = line.split("\\s+");
                String driveLetter = segments[0];
                String description = segments[1];
                String driveType = switch (segments[2]) {
                    case "2" -> "可移动存储(USB/SD卡)";
                    case "3" -> "本地磁盘(HDD/SSD)";
                    case "5" -> "光盘(CD/DVD)";
                    default -> "未知存储类型";
                };
                
                System.out.println("驱动器名称: " + driveLetter);
                System.out.println("描述: " + description);
                System.out.println("类型: " + driveType);
                System.out.println("---");
            }
            
            reader.close();
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

这种方法不需要依赖任何额外模块,还能直接区分驱动器的具体类型。

方案3:用Java NIO API获取基础驱动器信息

如果只需要基础的驱动器路径和空间信息,可以使用Java核心库的NIO API,无需引入额外模块:

import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Files;

public class DriveInfoNIO {
    public static void main(String[] args) {
        FileSystems.getDefault().getRootDirectories().forEach(rootDir -> {
            try {
                FileStore store = Files.getFileStore(rootDir);
                System.out.println("驱动器路径: " + rootDir);
                System.out.println("总容量: " + store.getTotalSpace() / (1024 * 1024 * 1024) + " GB");
                System.out.println("可用空间: " + store.getUsableSpace() / (1024 * 1024 * 1024) + " GB");
                
                // Windows下可通过存储名称判断是否为可移动设备
                boolean isRemovable = store.name().toLowerCase().contains("removable");
                System.out.println("是否为可移动存储: " + (isRemovable ? "是" : "否"));
                System.out.println("---");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}

这个方法兼容性强,所有Java版本都能直接运行,但无法获取官方描述文本。

内容的提问来源于stack exchange,提问作者Thomas Basile

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 07:33:20