如何在Java中打开文件夹/目录

如何在Java中打开文件夹/目录,java,file,directory,Java,File,Directory,我有一个带有imageView项的TilePane,每个imageView显示一个从我电脑目录中获取的图像。单击TilePane中的任何图像时,控制台上会打印一条消息,其中包含图像所在文件夹的目录路径地址,例如: You clicked: ImageResources/wp.png 我想进一步扩展它,以便在单击特定图像时,图像所在的文件夹打开 下面的我的实现只打印下面的消息,但没有打开任何目录/文件夹。信息是: File Not Found 我如何才能让它工作,使文件夹|目录打开?提前谢谢大

我有一个带有
imageView
项的TilePane,每个imageView显示一个从我电脑目录中获取的图像。单击
TilePane
中的任何图像时,控制台上会打印一条消息,其中包含图像所在文件夹的目录路径地址,例如:

You clicked: ImageResources/wp.png
我想进一步扩展它,以便在单击特定图像时,图像所在的文件夹打开

下面的我的实现只打印下面的消息,但没有打开任何目录/文件夹。信息是:

File Not Found
我如何才能让它工作,使文件夹|目录打开?提前谢谢大家

另外,目录中的一部分可以在选中单击的图像的情况下打开,这将是额外的,但现在不是优先级

到目前为止,我的实施情况如下:

public class TilePaneExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox(30);

        String[] imageResources = new String[]{
            //loading images
            "ImageResources/facebook.png",
            "ImageResources/faviicon.png",
            "ImageResources/jquery-logo.png",
            "ImageResources/linkedin_32.png",
            "ImageResources/loading1.png",
            "ImageResources/twitter.png",
            "ImageResources/twitter_32.png",
            "ImageResources/wp.png",};

        // Pane
        TilePane tilePane = new TilePane();
        tilePane.setHgap(5);
        tilePane.setVgap(5);

        for (final String imageResource : imageResources) {
            Image image = new Image(getClass().getResourceAsStream(imageResource));
            ImageView imageView = new ImageView(image);
            imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    File f = new File(imageResource);
                    String absolutePath = f.getAbsolutePath();
                    String folderPath = absolutePath.
                            substring(0, absolutePath.lastIndexOf(File.separator));
                    try {
                        Desktop.getDesktop().open(new File(folderPath));
                    } catch (IllegalArgumentException iae) {
                        System.out.println("File Not Found");
                    } catch (IOException ex) {
                        Logger.getLogger(TilePaneExample.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
            tilePane.getChildren().add(imageView);
        }

        root.getChildren().addAll(tilePane);
        primaryStage.setTitle("TilePane Example");
        Scene scene = new Scene(root, 300, 150);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
公共类TilePaneExample扩展应用程序{
@凌驾
公共无效开始(阶段primaryStage){
VBox根=新的VBox(30);
字符串[]图像资源=新字符串[]{
//加载图像
“ImageResources/facebook.png”,
“ImageResources/faviicon.png”,
“ImageResources/jquery logo.png”,
“ImageResources/linkedin_32.png”,
“ImageResources/loading1.png”,
“ImageResources/twitter.png”,
“ImageResources/twitter_32.png”,
“ImageResources/wp.png”,};
//窗格玻璃
TilePane TilePane=新的TilePane();
替利泮。setHgap(5);
tilePane.setVgap(5);
for(最终字符串imageResource:imageResources){
Image Image=新图像(getClass().getResourceAsStream(imageResource));
ImageView ImageView=新的ImageView(图像);
setOnMouseClicked(新的EventHandler(){
@凌驾
公共无效句柄(MouseeEvent事件){
文件f=新文件(imageResource);
字符串absolutePath=f.getAbsolutePath();
字符串folderPath=绝对路径。
子字符串(0,absolutePath.lastIndexOf(File.separator));
试一试{
getDesktop().open(新文件(folderPath));
}捕获(IllegalArgumentException iae){
System.out.println(“未找到文件”);
}捕获(IOEX异常){
Logger.getLogger(TilePaneExample.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
tilePane.getChildren().add(imageView);
}
root.getChildren().addAll(tilePane);
primaryStage.setTitle(“TilePane示例”);
场景=新场景(根,300,150);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

例如,单击第二个图像“ImageResources/faviicon.png”,将其文件夹路径打印为D:\standAloneDev\java\workingDir\Jive\TilePaneExample\ImageResources

运行时
文件夹路径的值是多少?例如,单击第二个图像“ImageResources/faviicon.png”
,将其文件夹路径打印为
D:\standAloneDev\java\workingDir\Jive\TilePaneExample\ImageResources
,@pphoenix可能是
IllegalArgumentException iae
的消息显示了问题所在。尝试
iae.printStackTrace()
而不是sysout。