熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java類使用類裝載器實現裝載文件

2022-06-13   來源: Java核心技術 

  view plainprint?
package combirddao;

  import javaioFileInputStream;
import javaioInputStream;
import javautilProperties;

  import orgjunitTest;

  /**
 * @use Java普通類實現裝載配置文件
 * @author Bird
 *
 */
public class UserDao {

  @Test//無法實現動態修改後呈現因為直接裝載到內存中是不會隨著磁盤的文件的更改而更改的
    public void update() throws Exception{
        InputStream in = UserDaoclassgetClassLoader()getResourceAsStream(dbproperties);//得到當前類裝載器裝載配置文件
        Properties pro = new Properties();
        proload(in);

  String url = progetProperty(url);
        String username = progetProperty(username);
        String password = progetProperty(password);

  Systemoutprintln(url);
        Systemoutprintln(username);
        Systemoutprintln(password);
    }

  @Test//可以時時的獲得更改後的數據
    public void test() throws Exception{
        String path = UserDaoclassgetClassLoader()getResource(dbproperties)getPath();
        FileInputStream in = new FileInputStream(path);
        Properties pro = new Properties();
        proload(in);
        String url = progetProperty(url);
        String username = progetProperty(username);
        String password = progetProperty(password);

  Systemoutprintln(url);
        Systemoutprintln(username);
        Systemoutprintln(password);
    }
}


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26445.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.