How to load a resource bundle from a file resource in Java?

前端 未结 14 800
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 08:19

I have a file called mybundle.txt in c:/temp -

c:/temp/mybundle.txt

How do I load this file into a java.util.Res

相关标签:
14条回答
  • I think that you want the file's parent to be on the classpath, not the actual file itself.

    Try this (may need some tweaking):

    String path = "c:/temp/mybundle.txt";
    java.io.File fl = new java.io.File(path);
    
    try {
       resourceURL = fl.getParentFile().toURL();
    } catch (MalformedURLException e) {
       e.printStackTrace();                     
    }               
    
    URLClassLoader urlLoader = new URLClassLoader(new java.net.URL[]{resourceURL});
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("mybundle.txt", 
                    java.util.Locale.getDefault(), urlLoader );
    
    0 讨论(0)
  • 2020-11-28 09:11

    When you say it's "a valid resource bundle" - is it a property resource bundle? If so, the simplest way of loading it probably:

    try (FileInputStream fis = new FileInputStream("c:/temp/mybundle.txt")) {
      return new PropertyResourceBundle(fis);
    }
    
    0 讨论(0)
  • 2020-11-28 09:11
    public class One {
    
        private static One one = null;
    
        Map<String, String> configParameter = Collections.synchronizedMap(new HashMap<String, String>());
    
        private One() {
            ResourceBundle rb = ResourceBundle.getBundle("System", Locale.getDefault());
    
            Enumeration en = rb.getKeys();
            while (en.hasMoreElements()) {
                String key = (String) en.nextElement();
                String value = rb.getString(key);
                configParameter.put(key, value);
    
            }
        }
    
        public static One getInstance() {
            if (one == null) {
                one= new One();
            }
    
            return one;
    
        }
    
        public Map<String, String> getParameter() {
    
            return configParameter;
        }
    
    
    
        public static void main(String[] args) {
            String string = One.getInstance().getParameter().get("subin");
            System.out.println(string);
    
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:11
    ResourceBundle rb = ResourceBundle.getBundle("service"); //service.properties
    System.out.println(rb.getString("server.dns")); //server.dns=http://....
    
    0 讨论(0)
  • 2020-11-28 09:15

    This worked for me very well. And it doesn't reload the bundle everytime. I tried to take some stats to load and reload the bundle from external file location.

    File file = new File("C:\\temp");
    URL[] urls = {file.toURI().toURL()};
    ClassLoader loader = new URLClassLoader(urls);
    ResourceBundle rb = ResourceBundle.getBundle("myResource", Locale.getDefault(), loader);
    

    where "c:\temp" is the external folder (NOT on the classpath) holding the property files, and "myResource" relates to myResource.properties, myResource_fr_FR.properties, etc.

    Note: If you have the same bundle name on your classpath then it will be picked up by default using this constructor of URLClassLoader.

    Credit to http://www.coderanch.com/t/432762/java/java/absolute-path-bundle-file

    Find some of the stats below, all time in ms. I am not worried about the initial load time as that could be something with my workspace or code that I am trying to figure out but what I am trying to show is the reload took way lesser telling me its coming from memory.

    Here some of the stats:

    • Initial Locale_1 load took 3486
    • Reload Locale_1 took 24
    • Reload Locale_1 took 23
    • Reload Locale_1 took 22
    • Reload Locale_1 took 15
    • Initial Locale_2 load took 870
    • Reload Locale_2 took 22
    • Reload Locale_2 took 18
    • Initial Locale_3 load took 2298
    • Reload Locale_3 took 8
    • Reload Locale_3 took 4
    0 讨论(0)
  • 2020-11-28 09:16

    1) Change the extension to properties (ex. mybundle.properties.)
    2) Put your file into a jar and add it to your classpath.
    3) Access the properties using this code:

    ResourceBundle rb = ResourceBundle.getBundle("mybundle");
    String propertyValue = rb.getString("key");
    
    0 讨论(0)
提交回复
热议问题