VIRGIN FOREST OF JAVA
不要埋头苦干,要学习,学习,再学习。。。。。
powered by R.Zeus

      before taday,I always read properties from file use by useing classes defined ,It is good luck for me to find the existent class to use when I read source of  log4j

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

public class test {

 //static Logger logger = Logger.getLogger(test.class);

 private static ClassLoader getTCL() throws IllegalAccessException,
   InvocationTargetException {

  // Are we running on a JDK 1.2 or later system?
  Method method = null;
  try {
   method = Thread.class.getMethod("getContextClassLoader", null);
  } catch (NoSuchMethodException e) {
   // We are running on JDK 1.1
   return null;
  }

  return (ClassLoader) method.invoke(Thread.currentThread(), null);
 }

 static public URL getResource(String resource) {
  ClassLoader classLoader = null;
  URL url = null;

  try {
   classLoader = getTCL();
  } catch (IllegalAccessException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }

  url = classLoader.getResource(resource);

  return url;

 }

 public static void main(String argv[]) {
  //  BasicConfigurator.configure();
  //  logger.debug("Hello world.");
  //  logger.info("What a beatiful day.");
  
  Properties props = new Properties();
  java.net.URL configURL;
  try {
   configURL = getResource("b.txt");
   props.load(configURL.openStream());
   String value = props.getProperty("a");
   System.out.print(value);
  } catch (MalformedURLException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  } catch (IOException e1) {
   // TODO 自动生成 catch 块
   e1.printStackTrace();
  }
 }
}

in properties file,# is remark

posted on 2005-08-12 01:04 R.Zeus 阅读(315) 评论(0)  编辑  收藏 所属分类: J2SE

只有注册用户登录后才能发表评论。


网站导航: