简体   繁体   English

JAR文件外部的JavaFX外部目录和资源

[英]JavaFX external directory and resources outside of jar file

I am new to Java and developing a desktop app with JavaFX. 我是Java新手,正在使用JavaFX开发桌面应用程序。 I created a folder in the project and place some images within. 我在项目中创建了一个文件夹,并在其中放置了一些图像。 The user may replace the images within the directory after deploying on the client. 在客户端上部署后,用户可以替换目录中的映像。 So far I tried, I can't find a way to do this. 到目前为止,我已经尝试过了,但是还找不到一种方法。 The directory I created are all complied inside to Jar File. 我创建的目录全部都包含在Jar File中。 The source structure : 源码结构:

在此处输入图片说明

The compiled Jar : 编译的Jar:

在此处输入图片说明

Opening the Jar file with archive manager: 使用存档管理器打开Jar文件:

在此处输入图片说明

I need to place the "dic_images" directory outside of jar and be able to access from codes so that user can replace the images within. 我需要将“ dic_images”目录放置在jar之外,并能够从代码进行访问,以便用户可以替换其中的图像。 I am using NetBeans IDE. 我正在使用NetBeans IDE。

The way I have accomplished this is like so: 我完成此操作的方式如下:

    //Make a dir using the users home directory and a filename.
    File dir = new File(System.getProperty("user.home") + "/NameOfDir/");
    //List the files in that dir
    File[] listOfFiles = dir.listFiles();

    //If there are no files, do stuff.
    if (listOfFiles == null) {
        //If the dir doesn't exist already, make it.
        if (!dir.exists()) {
            dir.mkdir();
        }
       //Do stuff in the empty dir
    } else {
        //List the file names in the dir
        List<String> fileNamesList = new ArrayList<String>(Arrays.asList(dir.list()));
        //Do stuff with the files list.
    }

This will make sure the directory exists, and if it doesn't it will make it. 这将确保目录存在,如果不存在,它将进行创建。 Then you can do whatever you need to do with the dir on the fly. 然后,您可以随时随地处理目录。

I've used it for slideshow images and videos with JavaFX. 我已经将它用于JavaFX的幻灯片图像和视频。 So good luck in your endeavors! 祝您一切顺利!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM