网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.介绍 WebLogic 的一些结构和特点.
.JAVA中通过JDBC访问MS SQL Serve.
.Java Bean 映射工具&n.
.JAR文件包及jar命令详解.
.详细介绍什么是实时JAVA.
.JScript 函数.
.Servlet技术及其与CGI的比较.
.通过三种方式对Struts框架进行扩.
.如何控制DataGrid里的内容换行与.
.JAgileSearch 1.0 alpha Done.
.Eclipse开发struts完全指南二(全.
.爪哇语言简单工厂创立性模式介绍.
.JBuilder2005单元测试体验之测试.
.Java数据库程序中的存储过程设计.
.用XSLT和XML改进Struts.
.谨慎使用单精度/双精度这两数值类.
.Java编程语言中创建和使用日期的.
.Win2000 Server下安装j2ee.
.osworkflow 小培训(1).
.Servlet运行环境所需的软件安装及.

enoeht的Java源码系列之处理配置文件

发表日期:2008-1-5



  我们经常会在程序中用到这样的配置文件:
  
  Listener = org.kyle.net.svr.sample.SampleListenerImpl
  
  ServerAddress = 127.0.0.1
  
  ListeningPort = 80
  
  ListenerTimeout = 120
  
  StatelessService = true
  
  LogLevel = ALL
  
  LogPath = server.log
  
  在这里提供了一个处理这种配置文件的类的源代码。
  
  package org.kyle.util;
  
  import Java.io.*;
  
  import java.util.*;
  
  //加载配置文件,并提供从配置文件中读取各种类型的值的方法
  
  public class Profile
  
  {
  
  protected Properties applicationProps;
  
  protected String m_configurationFilename = null;
  
  private boolean m_debug = false;
  
  public Profile( boolean debug)
  
  {
  
  this();
  
  m_debug = debug;
  
  }
  
  public Profile()
  
  {
  
  this(System.getProperty("MainConfigFile","Server.cfg"));
  
  }
  
  public Profile(String configurationFilename)
  
  {
  
  this.m_configurationFilename = configurationFilename;
  
  loadCfg(configurationFilename);
  
  }
  
  public void loadConfig(String configurationFilename)
  
  {
  
  if( configurationFilename == null )
  
  {
  
  System.exit(-1);
  
  }
  
  try {
  
  applicationProps = new Properties();
  
  FileInputStream in = new FileInputStream(configurationFilename);
  
  applicationProps.load(in);
  
  in.close();
  
  }
  
  catch( IOException ie)
  
  {
  
  System.exit(-1);
  
  }
  
  }
  
  public void loadConfig()
  
  {
  
  loadConfig( m_configurationFilename );
  
  }
  
  public void saveConfig()
  
  {
  
  try
  
  {
  
  FileOutputStream out = new FileOutputStream(m_configurationFilename);
  
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
  
  synchronized (applicationProps)
  
  {
  
  Iterator iterator = new TreeSet(applicationProps.keySet()).iterator();
  
  while(iterator.hasNext())
  
  {
  
  String key = (String)iterator.next();
  
  writer.write(key + "=" + applicationProps.getProperty(key));
  
  writer.newLine();
  
  }
  
  }
  
  writer.close();
  
  out.close();
  
  }catch(IOException ie)
  
  {
  
  System.out.println(ie.toString());
  
  }
  
  }
  
  public void showConfig()
  
  {
  
  applicationProps.list(System.out);
  
  }
  
  public Properties getProperty()
  
  {
  
  return applicationProps;
  
  }
  
  String getString(String Section, String key, String Default)
  
  {
  
  return getString( key, Default);
  
  }
  
  public String getString(String key, String Default)
  
  {
  
  String rVal = applicationProps.getProperty(key, Default);
  
  return rVal == null ? rVal : rVal.trim();
  
  }
  
  public String getString(String key)
  
  {
  
  String rVal = applicationProps.getProperty(key);
  
  return rVal == null ? rVal : rVal.trim();
  
  }
  
  public boolean getBoolean(String key, boolean Default)
  
  {
  
  String rVal = getString(key);
  
  //  if (rVal == null) return Default;
  
  if ("true".equalsIgnoreCase(rVal)) return true;
  
  if ("false".equalsIgnoreCase(rVal)) return false;
  
  return Default;
  
  }
  
  public int getInt(String key, int Default)
  
  {
  
  try{
  
  return getInt(key);
  
  }catch(Exception e){
  
  applicationProps.setProperty(key, String.valueOf(Default));
  
  return Default;
  
  }
  
  }
  
  protected int getInt(String key) throws NumberFormatException
  
  {
  
  String rVal = getString(key);
  
  return Integer.parseInt(rVal);
  
  }
  
  public String getConfigurationFilename()
  
  {
  
  return m_configurationFilename;
  
  }
  
  private void loadCfg(String configurationFilename)
  
  {
  
  if( configurationFilename == null )
  
  {
  
  System.out.println("Assigned a null configuration file. Default setting used.");
  
  }
  
  try
  
  {
  
  applicationProps = new Properties();
  
  FileInputStream in = new FileInputStream(configurationFilename);
  
  applicationProps.load(in);
  
  in.close();
  
  }
  
  catch( IOException ioe)
  
  {
  
  System.out.println("Loading configuration from file " + configurationFilename + " failed.");
  
  System.out.println("Default setting will be used.");
  
  }
  
  }
  
  }
  
  package org.kyle.util;
  
  import java.net.*;
  
  //调用父类加载配置文件和读取数据,按照配置文件的中的key值读取其value。
  
  public class GenProfile extends Profile
  
  {
  
  public GenProfile()
  
  {
  
  super();
  
  buildCachedCrypt();
  
  }
  
  public GenProfile( String cfgFileName )
  
  {
  
  super( cfgFileName );
  
  buildCachedCrypt();
  
  }
  
  public String getListenerImpl()
  
  {
  
  return getString("Listener", " org.kyle.net.svr.sample.SampleListenerImpl");
  
  }
  
  public InetAddress getServerAddress()
  
  {
  
  try
  
  {
  
  String svrAddr = getString("ServerAddress",null);
  
  if ( svrAddr == null ) return null;
  
  return InetAddress.getByName( svrAddr );
  
  }
  
  catch( UnknownHostException uhe)
  
  {
  
  Debug.info(uhe);
  
  }
  
  return null;
  
  }
  
  public int getListenAt()
  
  {
  
  return getInt("ListeningPort", 80);
  
  }
  
  public int getTimeout()
  
  {
  
  return getInt("ListenerTimeout", 120);
  
  }
  
  public boolean statelessService()
  
  {
  
  return getBoolean("StatelessService", true );
  
  }
  
  public String getLogLevel()
  
  {
  
  return getString("LogLevel","CONFIG");
  
  }
  
  public String getLogPath()
  
  {
  
  return getString("LogPath","server.log");
  
  }
  
  }
  
  使用方法:
  String cfgFile ="server.cfg";
  
  GenProfile m_env = new GenProfile( cfgFile );
  
  这样在程序中就可以使用例如m_env. getServerAddress()等方法取得配置文件的相应内容了。
上一篇:教您如何在Linux下配置Java开发环境详述 人气:735
下一篇:Java初学者讲堂:JavaBean简易入门 人气:602
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐