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

前端 未结 14 859
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 08:54

    As long as you name your resource bundle files correctly (with a .properties extension), then this works:

    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.

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

提交回复
热议问题