JAVA之讀取properties時路徑的注意問題
JAVA讀取properties時路徑的注意
先來看看建立的測試工程目錄
屬性文件我們放在包test下,當然了,一般在實際開發(fā)過程中不建議這樣做,建立把屬性文件放在src目錄下,現(xiàn)在放在包下主要是便于了解路徑的問題。
下面來看一段讀取屬性文件的代碼,屬性文件配置了一個類Hello的K-V鍵值,我們要從中讀取并加載到內(nèi)存中來。
ReadProperties.properties
v=com.luhy.test.Hello
Hello類:
package com.luhy.test; public class Hello { public void run(){ System.out.println("Hello"); } }
ReadProperties.java
package com.luhy.test; import java.util.Properties; public class ReadProperties { public static void main(String[] args) throws Exception{ String filename = "com/luhy/test/ReadProperties.properties"; Properties props = new Properties(); props.load(ReadProperties.class.getClassLoader().getResourceAsStream(filename)); String h = props.getProperty("v"); Object o = Class.forName(h).newInstance(); Hello hello = (Hello)o; hello.run(); } }
執(zhí)行完打印輸出:
Hello
下面再來看一下編譯后的bin目錄
可見編譯后屬性文件被自動放到相應的包內(nèi),當然了,這里的bin相當于源碼中的src,實際開發(fā)中一般放在此src目錄下,這樣在發(fā)布項目時就不用折騰了。
說明:
props.load(ReadProperties.class.getClassLoader().getResourceAsStream(filename));
意思是獲得從Properties類獲得類加載器(類加載器主要有四種,分別加載不同類型的類,加載只是把class文件放進內(nèi)存,并沒有產(chǎn)生對象),并把指定文件轉(zhuǎn)化為流。
這一步,有很多新手,直接往load()里填文件名或具體文件路徑名,程序運行時會報錯找不到指定路徑。
所以,一定要注意這點。
JAVA讀取properties文件,中文出現(xiàn)亂碼
問題的提出
初用properties,讀取java properties文件的時候如果value是中文,會出現(xiàn)讀取亂碼的問題
問題分析
開始以為是文件保存編碼問題,把eclipse中所有的文件編碼都修改成utf8,問題依然存在;
把內(nèi)容復制到notepad++進行utf8編碼轉(zhuǎn)換,問題依舊;
上網(wǎng)搜索有人提議重寫properties類或者用jdk自帶的編碼轉(zhuǎn)換工具,嫌麻煩而且憑感覺jdk開發(fā)者不可能不考慮東亞幾國的字符編碼問題;
因為properties文件操作的代碼是參考百度文庫里的一邊文章的,分析其代碼后,發(fā)現(xiàn)其用的是字節(jié)流來讀取文件,
具體代碼如下:
Properties properties = new Properties(); ? InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties"); ? properties.load(inputStream ); ? System.out.println(properties.getProperty("a")); ?
因為字節(jié)流是無法讀取中文的,所以采取reader把inputStream轉(zhuǎn)換成reader用字符流來讀取中文。
代碼如下:
Properties properties = new Properties(); ? InputStream inputStream = this.getClass().getResourceAsStream("/menu.properties"); ? BufferedReader bf = new BufferedReader(new ? ?InputStreamReader(inputStream)); ? properties.load(bf); ? System.out.println(properties.getProperty("a")); ?
代碼示例
1、工具類
/** ?* 讀取配置文件Properties ?*? ?* @author xl ?* ?*/ public class PropertiesUtil { ?? ?private PropertiesUtil() { ?? ?} ?? ?private static class SingletonHolder { ?? ??? ?private final static PropertiesUtil instance = new PropertiesUtil(); ?? ?} ?? ?public static PropertiesUtil getInstance() { ?? ??? ?return SingletonHolder.instance; ?? ?} ?? ?/** ?? ? * 讀取key字段,配置文件在classes根路徑下xx.properties,在子路徑下xx/xx.properties ?? ? *? ?? ? * @param file ?? ? * @param key ?? ? * @return ?? ? */ ?? ?public String read(String file, String key) { ?? ??? ?InputStream in = getClass().getClassLoader().getResourceAsStream(file); ?? ??? ?BufferedReader bf = new BufferedReader(new InputStreamReader(in));? ?? ??? ?Properties prop = new Properties(); ?? ??? ?String value = ""; ?? ??? ?try { ?? ??? ??? ?prop.load(bf); ?? ??? ??? ?value = prop.getProperty(key); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ?return value; ?? ?} }
2、使用工具類
/** ?? ? * 獲取properties文件中存放的數(shù)據(jù) ?? ? *? ?? ? * @param req ?? ? * @param resp ?? ? * @return ?? ? * @throws Exception ?? ? */ ?? ?@RequestMapping(value = "/getPropertiesData") ?? ?@ResponseBody ?? ?public Map<String, Object> getPropertiesData(HttpServletRequest req, HttpServletResponse resp) throws Exception { ?? ??? ?Map<String, Object> returnMap = new HashMap<String, Object>(); ?? ??? ?// 獲取請求參數(shù) ?? ??? ?String key = (String) req.getParameter("key"); ?? ??? ?String file = (String) req.getParameter("file"); ?? ??? ?// 從配置文件讀取key對應的value ?? ??? ?String value = PropertiesUtil.getInstance().read(file, key); ?? ??? ?returnMap.put("data", value); ?? ??? ?// 解析返回結(jié)果 ?? ??? ?return returnMap; ?? ?}
完?。?!
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
IDEA實現(xiàn) springmvc的簡單注冊登錄功能的示例代碼
這篇文章主要介紹了IDEA實現(xiàn) springmvc的簡單注冊登錄功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06