Java讀取Properties文件幾種方法總結(jié)
使用J2SE API讀取Properties文件的六種方法
1。使用Java.util.Properties類的load()方法
示例:
InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in);
2。使用java.util.ResourceBundle類的getBundle()方法
示例:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle類的構(gòu)造函數(shù)
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class變量的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in);
6。使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態(tài)方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in);
補充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path); Properties p = new Properties(); p.load(in);
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
SpringBoot/Spring?AOP默認動態(tài)代理方式實例詳解
這篇文章主要給大家介紹了關(guān)于SpringBoot/Spring?AOP默認動態(tài)代理方式的相關(guān)資料,Spring AOP是一款基于Java的AOP框架,其中默認采用動態(tài)代理方式實現(xiàn)AOP功能,本文將詳細介紹動態(tài)代理的實現(xiàn)原理和使用方法,需要的朋友可以參考下2023-03-03Java HttpClient實現(xiàn)socks代理的示例代碼
這篇文章主要介紹了Java HttpClient 實現(xiàn) socks 代理的示例代碼,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11