今天在mybatis中想讀取文件內容,一開始是創建的xml文件,發現必須要表頭,不然就是<beans>;完全不適用,然後突然靈光一現,可以創建properties文件,讀取文件,通過鍵值對獲取內容。
Properties proper = new Properties();
proper.load(this.getClass().getResourceAsStream("/xx.properties"));//讀取文件,我保存文件的位置在src/main/resource
Map<String, String> map = new HashMap<String, String>();
Iterator<Object> keys = proper.keySet().iterator();
while (keys.hasNext()) {//遍歷文件內容
String key = (String) keys.next();
if (proper.getProperty(key) != null) {
map.put(key, proper.getProperty(key).toString());//保存在map中
}
}
結果爲:a=1,b=2 想要獲取a的值:map.get("a").toString()即可