我有一个名为mybundle.txt在c:/temp-
c:/temp/mybundle.txt
如何将此文件加载到java.util.ResourceBundle?该文件是有效的资源包.
这似乎不起作用:
java.net.URL resourceURL = null;
String path = "c:/temp/mybundle.txt";
java.io.File fl = new java.io.File(path);
try {
   resourceURL = fl.toURI().toURL();
} catch (MalformedURLException e) {             
}           
URLClassLoader urlLoader = new URLClassLoader(new java.net.URL[]{resourceURL});
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle( path , 
                java.util.Locale.getDefault(), urlLoader );
For*_*e_7 62
只要您正确命名资源包文件(扩展名为.properties),就可以了:
File file = new File("C:\\temp");
URL[] urls = {file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("myResource", Locale.getDefault(), loader);
其中"c:\ temp"是包含属性文件的外部文件夹(不在类路径上),"myResource"与myResource.properties,myResource_fr_FR.properties等相关.
感谢http://www.coderanch.com/t/432762/java/java/absolute-path-bundle-file
Jon*_*eet 44
当你说它是"有效的资源包"时 - 它是属性资源包吗?如果是这样,最简单的加载方式可能是:
try (FileInputStream fis = new FileInputStream("c:/temp/mybundle.txt")) {
  return new PropertyResourceBundle(fis);
}
Lud*_*ton 21
1)将扩展名更改为属性(例如mybundle.properties.)
2)将文件放入jar中并将其添加到类路径中.
3)使用以下代码访问属性:
ResourceBundle rb = ResourceBundle.getBundle("mybundle");
String propertyValue = rb.getString("key");
Nic*_*olt 11
来自JavaDocs ResourceBundle.getBundle(String baseName):
baseName- 资源包的基本名称,完全限定的类名
这意味着简单的英语是资源包必须在类路径上,并且baseName应该是包含bundle和包名称的包,mybundle在你的情况下.
不要使用扩展名和构成包名称一部分的任何语言环境,JVM将根据默认语言环境为您排序 - 有关详细信息,请参阅java.util.ResourceBundle上的文档.
对于JSF应用程序
从给定的文件路径获取资源包prop文件以在JSF应用程序中使用它们.
basename属性中指定类loadBundle.
<f:loadBundle    basename="Message" var="msg" />有关扩展RB的基本实现,请参阅样本定制资源包中的示例
/* Create this class to make it base class for Loading Bundle for JSF apps */
public class Message extends ResourceBundle {
        public Messages (){
                File file = new File("D:\\properties\\i18n");  
                ClassLoader loader=null;
                   try {
                       URL[] urls = {file.toURI().toURL()};  
                       loader = new URLClassLoader(urls); 
                       ResourceBundle bundle = getBundle("message", FacesContext.getCurrentInstance().getViewRoot().getLocale(), loader);
                       setParent(bundle);
                       } catch (MalformedURLException ex) { }
       }
      .
      .
      .
    }
否则,从getBundle方法获取bundle,但从其他源获取locale,Locale.getDefault()在这种情况下可能不需要new(RB)类.
如果像我一样,您实际上想从文件系统而不是类路径加载 .properties 文件,但保留与查找相关的所有智能,请执行以下操作:
java.util.ResourceBundle.ControlnewBundle()方法在这个愚蠢的示例中,我假设您有一个文件夹,其中C:\temp包含“.properties”文件的平面列表:
public class MyControl extends Control {
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
        throws IllegalAccessException, InstantiationException, IOException {
    if (!format.equals("java.properties")) {
        return null;
    }
    String bundleName = toBundleName(baseName, locale);
    ResourceBundle bundle = null;
    // A simple loading approach which ditches the package      
    // NOTE! This will require all your resource bundles to be uniquely named!
    int lastPeriod = bundleName.lastIndexOf('.');
    if (lastPeriod != -1) {
        bundleName = bundleName.substring(lastPeriod + 1);
    }
    InputStreamReader reader = null;
    FileInputStream fis = null;
    try {
        File file = new File("C:\\temp\\mybundles", bundleName);
        if (file.isFile()) { // Also checks for existance
            fis = new FileInputStream(file);
            reader = new InputStreamReader(fis, Charset.forName("UTF-8"));
            bundle = new PropertyResourceBundle(reader);
        }
    } finally {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(fis);
    }
    return bundle;
}
}
另请注意,这支持 UTF-8,我相信默认情况下不支持 UTF-8。
| 归档时间: | 
 | 
| 查看次数: | 201468 次 | 
| 最近记录: |