我有一小段代码
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/