文件名称:Java学习测试题,看你会多少(主要基础类)
文件大小:24KB
文件格式:TXT
更新时间:2012-05-29 16:42:29
java 学习
例:做一个单子模式的类,只加载一次属性文件 package com.softeem.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** *@authorleno *单子模式,保证在整个应用期间只加载一次配置属性文件 */ publicclass Singleton { privatestatic Singleton instance; privatestaticfinal String CONFIG_FILE_PATH = "E:\\config.properties"; private Properties config; private Singleton() { config = new Properties(); InputStream is; try { is = new FileInputStream(CONFIG_FILE_PATH); config.load(is); is.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } publicstatic Singleton getInstance() { if(instance==null) { instance = new Singleton(); } returninstance; } public Properties getConfig() { returnconfig; } publicvoid setConfig(Properties config) { this.config = config; } }