ITeye博客

ResourceBundle和Properties

CunningFox · 2013-01-09 15:56 · Web前端 · Markdown

项目需要从绝对路径中取的页面label的value

1.用ResourceBundle取值

public static void main(String args) throws MalformedURLException {
    String resourcePath = "D:/"; 
    String resourceFileName = "test"; 
    Locale currentLocale = Locale.getDefault(); 
    File resourceFile = new File(resourcePath); 
    URL resourceUrl = resourceFile .toURI().toURL(); 
    URL urls = {resourceUrl };   
    ClassLoader loader = new URLClassLoader(urls);   
    ResourceBundle bundle = ResourceBundle.getBundle(resourceFileName, currentLocale, loader);
    
    System.out.println(bundle.getString("viewRatings"));
}
  1. 直接IO读取properties文件 public static void main(String args) throws FileNotFoundException, IOException { InputStream is = new FileInputStream(new File("D:/test.properties")); Properties p = new Properties(); p.load(is);

     System.out.println(p.get("viewRatings"));
    

    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。