简体   繁体   中英

JavaFX external directory and resources outside of jar file

I am new to Java and developing a desktop app with 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. The source structure :

在此处输入图片说明

The compiled Jar :

在此处输入图片说明

Opening the Jar file with archive manager:

在此处输入图片说明

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. I am using 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. So good luck in your endeavors!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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