1

I have a Java application using Guice and JavaFX that requires configurable resources.

I am trying to load multiple resources using https://github.com/dueni/faces-ext/blob/master/resourcebundle/src/main/java/ch/dueni/util/MultiplePropertiesResourceBundle.java

I am able to load the two resource files from the JAR, but I want to be able to load them from a local directory where a user has access to modify them.

I have tried sending them to AppData, but I would prefer the file to be somewhere more configurable like the installation folder. I also do not know if its possible to load properties files from outside of the package.

This is currently how to loads the resources using ClassLoader:

      ClassLoader cl = Thread.currentThread().getContextClassLoader();
      List<String> bundleNames = new ArrayList<String>();
      try {
         String baseFileName = baseName + ".properties";
         String resourcePath = getResourcePath();
         String resourceName = resourcePath + baseFileName;
         if (isLoggable) {
            LOG.logp(Level.FINE, CLASS, METHOD, "Looking for files named '" + resourceName + "'");
         }
         Enumeration<URL> names = cl.getResources(resourceName);

I want to be able to get the names by a custom function which searches a designated directory for resource files.

Is there anyway this is possible?

1 Answers1

0

Yes, this is possible. I hope I understood your problem correctly.

1) Create the file with the path you want, example:

settingsFile = new File(System.getProperty("user.home") + "/{Project}/settings.xml");
settingsFile.getParentFile().mkdirs();
settingsFile.createNewFile();

The path leads to the user's default directory (For me, C:\users\meiswjn{Project}) but you can use any.

2) Set default values with properties.put(..).

3) Save the properties.

FileWriter fileWriter = new FileWriter(settingsFile);
BufferedWriter propWriter = new BufferedWriter(fileWriter);
props.store(propWriter, "Comment");
propWriter.close();
fileWriter.close();

4) You can read the properties with:

FileReader fileReader = new FileReader(settingsFile);
BufferedReader reader = new BufferedReader(fileReader);
props.load(reader);
fileReader.close();
reader.close();

Hope this helps.

Meiswjn
  • 752
  • 6
  • 17
  • 1
    Thank you for the reply. Well, it does help but maybe not in the way that I intended. My issue is that I am trying to load ResourceBundles from an external directory from the Jar. I think I may have came to a solution by using PropertyResourceBundle on a FileInputStream. – Quade Kayle Sep 06 '19 at 15:01