属性文件读取类

package com.yanek.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;


public final class Global {
 
 
 public static void main(String[] args)
 {
  
  System.out.println(Global.exptype);
  System.out.println(Global.descDic);
  System.out.println(Global.sourceDic);
 }
 
 
 /** 日志记录 */
 static Logger log = Logger.getLogger(Global.class.getName());

 /** 应用程序配置参数缓存 */
 private static Properties appPropertiesCache;

 /** 服务配置文件名 */

 //private static String config_file = "conf/mail.properties";
 private static String config_file = "config.properties";

 
 public static String APPLICATION_ROOT = null;
 
 
 public static String exptype = null; //出库类型
 
 public static String sourceDic = null; //来源目录
 public static String descDic = null; //目标目录

 static {
  try {
   APPLICATION_ROOT = System.getProperty("user.dir");
   System.out.println("properties file:" + config_file);
   appPropertiesCache = loadProperties(config_file);
   

 
   
   sourceDic = appPropertiesCache.getProperty("sourceDic");
   descDic=appPropertiesCache.getProperty("descDic");
   
   exptype=appPropertiesCache.getProperty("exptype");
   
   
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 /***********************************************************************************************
  * 根据给定名字查找资源,返回其输入流
  *
  * @param uri
  *            资源名
  * @return 输入流
  */
 public static InputStream getResourceAsStream(String uri) {
  return Global.class.getClassLoader().getResourceAsStream(uri);
 }

 /***********************************************************************************************
  * 载入属性文件
  *
  * @param 文件基于类路径的相对路径名
  * @return 根据属性文件生成的Properties对象
  * @throws IOException
  */
 public static Properties loadProperties(String uri) throws IOException {
  Properties result = null;
  InputStream in = getResourceAsStream(uri);
  if (in != null) {
   try {
    result = new Properties();
    result.load(in);
   } finally {
    
    in.close();
    //ResourceMgr.closeQuietly(in);
   }
  } else
   throw new IOException("载入属性文件失败!" + uri);
  return result;
 }

 /***********************************************************************************************
  * 根据指定键值取应用程序配置属性
  *
  * @param key
  *            该属性在配置文件中的键
  * @return 该属性在配置文件中的字符串值
  */
 public static String getAppProperty(String key) {
  String value = Global.appPropertiesCache.getProperty(key);
  if (value == null)
   value = System.getProperty(key);
  return value;
 }

}

 

注意:配置文件在src根目录即可

 

config.properties

 

exptype=2
sourceDic=C://artistExp//in
descDic=C://artistExp//out
#descDic=192.168.61.172//temp
#descDic=192.168.61.172//temp
systype=new

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值