java - 打开目录

标签 java directory desktop

我有一小段代码

File dir = new File("./brieven");
try {
    if (Desktop.isDesktopSupported()) {
        Desktop.getDesktop().open(dir);
    }
} catch (IOException exc) {
    exc.printStackTrace();
}

它在 Windows 7 上运行良好,但是当我在 Windows 8 上尝试它时,它会给出 Java.to.IOException 并显示错误消息系统找不到指定的文件

所以我的问题是,我需要做什么才能在 Windows 8 和 Windows 7 中打开该目录

编辑:

Path dir = Paths.get("./brieven");

if (Files.exists(dir) && Files.isDirectory(dir)) {
    try {
        if (Desktop.isDesktopSupported()) {
            Desktop.getDesktop().open(dir.toFile);
        }
    } catch (IOException exc) {
        exc.printStackTrace();
    }
}

编辑2: 现在尝试使用 toRealPath() 但程序停止工作。

try {
    Path dir = Paths.get("./brieven").toRealPath();
    if (Files.exists(dir) && Files.isDirectory(dir) && Desktop.isDesktopSupported()) {
        Desktop.getDesktop().open(dir.toFile());
    }
} catch (IOException exc) {
    exc.printStackTrace();
}

编辑3:在另一台装有Windows 8的笔记本电脑上尝试过,效果很好

最佳答案

try {
    Path dir = Paths.get("./brieven").toRealPath();
    if (Files.exists(dir) && Files.isDirectory(dir) && Desktop.isDesktopSupported()) {
        Desktop.getDesktop().open(dir.toFile());
    }
} catch (IOException exc) {
    exc.printStackTrace();
}

关于java - 打开目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22906398/

相关文章:

javascript - 如何使用 Node.js 检查目录是否为空?

linux - 创建Linux桌面环境

c# - 如何从 sql server 播放 mp3 字节 block ,使用 C# 传输到桌面客户端(通过 wcf)?

java - Tomcat 服务器出现 "Java(TM) Platform SE binary has stopped working"错误

java - 如何在 Spring Security 中将未找到 header 异常重定向到登录页面

java - 行数不相等的多个文件读取

java - pk使用 `pkexec` 打开 .jar 文件并使用桌面条目运行命令

java - Android 7.1 及更低版本显示双通知

c# - 将文件夹(目录)从一个位置 move 到另一个位置 - 不当行为

c# - DirectoryInfo.GetFiles,如何在 C# 中获取不同类型的文件