北大青鸟佳音旗舰

Java读取配置文件属性

点击数: 更新时间:2010-05-21 15:58:26
 

package com.chenwl;

  import java.io.FileInputStream;

  import java.io.FileNotFoundException;

  import java.io.IOException;

  import java.io.InputStream;

  import java.util.Properties;

  public class AppProperties {

  private static Properties prop = null;

  static {

  prop = new Properties();

  try {

  //1种指定文件的位置

  //InputStream inputStream = new FileInputStream("E:\\workspace\\java\\src\\config.properties");

  //2种得到classload来加载配置文件,不用在关心文件"config.properties"的具体位置

  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

  InputStream inputStream = classLoader.getResourceAsStream("config.properties");

  prop.load(inputStream);

  } catch (FileNotFoundException e) {

  e.printStackTrace();

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  public static String getProperties(String key){

  String value =prop.getProperty(key);

  if(value==null){

  value = "";

  }

  return value.trim();

  }

  public static void main(String[] args) {

  String ip = AppProperties.getProperties("ip");

  System.out.println(ip);

  }

  }

 


            

上一篇:C#实现自动锁屏+关屏[ 05-21 ]下一篇:浅谈C#编程 泛型编程的好处[ 05-21 ]
相关信息
没有相关内容
©Copyright2004 - 2009 www.bjaccp.com, All Rights Reserved
版权所有2004-2008 北大青鸟APTECH( 北京佳音旗舰 ) 授权培训中心
地址:北京西城区北礼士路100号( 阜成门华联商厦西门北侧北走50米)100037北大青鸟地址
京ICP备06064589号
51.la