---
title: "ResourceBundle和Properties"
author: "CunningFox"
date: "2013-01-09T15:56:21Z"
tags: []
categories: ["Web前端"]
slug: "resourcebundle-properties"
draft: false
lastmod: "2013-01-09T15:56:15Z"
id: 1766903
url: "https://cunningfox.iteye.com/blog/1766903"
---

项目需要从绝对路径中取的页面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"));
    }

2. 直接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"));
    }