我有一小段代码
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
一个错误消息The System couldn't find the file specified
所以我的问题是,如何在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的笔记本电脑上试用它,它运行良好
答案 0 :(得分:0)
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();
}