Java中Properties类的操作
生活随笔
收集整理的這篇文章主要介紹了
Java中Properties类的操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java中Properties類的操作
?
?
?=============================================
?
?
com.gordon.constant
Constant.java:
package com.gordon.constant;public interface Constant {/*** 根據(jù)名稱讀取常量* @param name* @return*/public String get_constant(String name); }?
SystemConstant4PropImpl.java
package com.gordon.constant;import java.io.IOException; import java.io.InputStream; import java.util.Properties;public class SystemConstant4PropImpl implements Constant {public Properties prop = null;public SystemConstant4PropImpl() {InputStream in = ClassLoader.getSystemResourceAsStream("constant.properties");prop = new Properties();try {prop.load(in);} catch (IOException e) {e.printStackTrace();}}public SystemConstant4PropImpl(String prop_path) {InputStream in = ClassLoader.getSystemResourceAsStream(prop_path);prop = new Properties();try {prop.load(in);} catch (IOException e) {e.printStackTrace();}}/*** 獲取properties文件中的屬性*/@Overridepublic String get_constant(String name) {return prop.getProperty(name);} }?
SystemConstant4XMLImpl.java
package com.gordon.constant;public class SystemConstant4XMLImpl {}?
com.grodon.test
TestConstantClass.java:
package com.gordon.test;import org.junit.Test;import com.gordon.constant.SystemConstant4PropImpl;public class TestConstantClass {@Testpublic void run() {try {SystemConstant4PropImpl scp = new SystemConstant4PropImpl();String db_username = scp.get_constant("db.username");System.out.println(db_username);} catch (Exception e) {e.printStackTrace();}} }?
constant.properties:
db.username=root?
運行結(jié)果:
?
轉(zhuǎn)載于:https://www.cnblogs.com/hfultrastrong/p/7611536.html
總結(jié)
以上是生活随笔為你收集整理的Java中Properties类的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle 常用sql整理
- 下一篇: 一个简单粗暴的爬虫 - 必应今日美图