環(huán)境:eclipse
步驟一:
創(chuàng)建Maven項目
步驟二:
創(chuàng)建PropertiesUtil類:
package Tool;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil{
//輸出name屬性文件內(nèi)容
public static void outputvalusofProperty(String name) throws IOException{
InputStream in=ClassLoader.getSystemResourceAsStream(name);
Properties pro =new Properties();
pro.load(in);
for (String key:pro.stringPropertyNames()){
System.out.println("key:"+key+" "+"value:"+pro.getProperty(key));
}
}
//獲取filename屬性文件中,關(guān)鍵字為key的值
public static String getProperty(String filename,String key) throws IOException{
InputStream in=ClassLoader.getSystemResourceAsStream(filename);
Properties pro =new Properties();
pro.load(in);
if(pro.containsKey(key))
return pro.getProperty(key);
else
return null;
}
}