Chinaunix首页 | 论坛 | 博客
  • 博客访问: 619747
  • 博文数量: 98
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 1528
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-28 16:20
文章分类

全部博文(98)

文章存档

2011年(1)

2010年(11)

2009年(44)

2008年(42)

我的朋友

分类: Java

2009-01-05 23:15:57

  1. import java.io.File;      
  2. import java.io.FileInputStream;      
  3.      
  4. import java.util.Properties;      
  5.      
  6.      
  7.      
  8.      
  9.      
  10.      
  11.      
  12. public class ReadConfigation{      
  13.               
  14.     /**    
  15.     * 属性文件全名    
  16.     */     
  17.     private static final String PFILE ="Config.properties";      
  18.     /**    
  19.     * 对应于属性文件的文件对象变量    
  20.     */     
  21.     private  File m_file = null;      
  22.     /**    
  23.     * 属性文件的最后修改日期    
  24.     */     
  25.     private  long m_lastModifiedTime = 0;      
  26.     /**    
  27.     * 属性文件所对应的属性对象变量    
  28.     */     
  29.     private  Properties m_props = null;      
  30.     /**    
  31.     * 本类可能存在的惟一的一个实例    
  32.     */     
  33.     private static ReadConfigation m_instance =new ReadConfigation();      
  34.     /**    
  35.     * 私有的构造子,用以保证外界无法直接实例化    
  36.     */     
  37.     private ReadConfigation()      
  38.     {      
  39.         m_file = new File(PFILE);      
  40.         m_lastModifiedTime = m_file.lastModified();      
  41.         if(m_lastModifiedTime == 0){      
  42.             System.err.println(PFILE +" file does not exist!");      
  43.         }      
  44.         m_props = new Properties();      
  45.         try     
  46.         {      
  47.             m_props.load(new FileInputStream(PFILE));      
  48.         }      
  49.         catch(Exception e)      
  50.         {      
  51.             e.printStackTrace();      
  52.         }      
  53.     }      
  54.     /**    
  55.     * 静态工厂方法    
  56.     * @return 返还ReadConfigation 类的单一实例    
  57.     */     
  58.     synchronized public static ReadConfigation getInstance()      
  59.     {      
  60.         return m_instance;      
  61.     }      
  62.     /**    
  63.     * 读取一特定的属性项    
  64.     *    
  65.     * @param name 属性项的项名    
  66.     * @param defaultVal 属性项的默认值    
  67.     * @return 属性项的值(如此项存在), 默认值(如此项不存在)    
  68.     */     
  69.      public  String getConfigItem(String name,String defaultVal)      
  70.     {      
  71.         long newTime = m_file.lastModified();      
  72.         // 检查属性文件是否被其他程序      
  73.         // 如果是,重新读取此文件      
  74.           
  75.         if(newTime == 0)      
  76.         {      
  77.             // 属性文件不存在      
  78.             if(m_lastModifiedTime == 0){      
  79.                 System.err.println(PFILE+ " file does not exist!");      
  80.             }else{      
  81.                 System.err.println(PFILE+ " file was deleted!!");      
  82.             }      
  83.             return defaultVal;      
  84.         }else if(newTime > m_lastModifiedTime){      
  85.             // Get rid of the old properties      
  86.             m_props.clear();      
  87.             try     
  88.             {      
  89.                 m_props.load(new FileInputStream(PFILE));      
  90.             }catch(Exception e){      
  91.                 e.printStackTrace();      
  92.             }      
  93.         }      
  94.             m_lastModifiedTime = newTime;      
  95.             String val = m_props.getProperty(name);      
  96.             if( val == null )      
  97.             {      
  98.                 return defaultVal;      
  99.             }      
  100.             else     
  101.             {      
  102.                 return val;      
  103.             }      
  104.     }     
  105. /**    
  106.     * 读取一特定的属性项    
  107.     *    
  108.     * @param name 属性项的项名    
  109.     * @return 属性项的值(如此项存在), 空(如此项不存在)    
  110.     */       
  111.    public   String getConfigItem(String name){      
  112.         return getConfigItem(name,"");      
  113.     }      
  114. }    
阅读(690) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~