简体   繁体   English

如何从本地目录而不是Java中的JAR加载resources.properties?

[英]How to load resources.properties from local directory, not the JAR in Java?

I have a Java application using Guice and JavaFX that requires configurable resources. 我有一个使用Guice和JavaFX的Java应用程序,它需要可配置的资源。

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 我正在尝试使用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. 我可以从JAR加载两个资源文件,但是我希望能够从用户有权修改它们的本地目录中加载它们。

I have tried sending them to AppData, but I would prefer the file to be somewhere more configurable like the installation folder. 我曾尝试将它们发送到AppData,但我希望该文件在安装文件夹等位置更具可配置性。 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加载资源的方法:

      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? 反正有可能吗?

Yes, this is possible. 是的,这是可能的。 I hope I understood your problem correctly. 希望我能正确理解您的问题。

1) Create the file with the path you want, example: 1)使用所需的路径创建文件,例如:

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. 该路径指向用户的默认目录(对我来说,是C:\\ users \\ meiswjn {Project}),但是您可以使用任何目录。

2) Set default values with properties.put(..). 2)使用properties.put(..)设置默认值。

3) Save the properties. 3)保存属性。

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: 4)您可以通过以下方式读取属性:

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

Hope this helps. 希望这可以帮助。

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

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