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

2016-02-19 13:39 0 1 收藏

get新技能是需要付出行动的,即使看得再多也还是要动手试一试。今天图老师小编跟大家分享的是enoeht的Java源码系列之处理配置文件,一起来学习了解下吧!

【 tulaoshi.com - 编程语言 】

我们经常会在程序中用到这样的配置文件:
  
  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()等方法取得配置文件的相应内容了。

来源:https://www.tulaoshi.com/n/20160219/1604887.html

延伸阅读
[SETUP]CpuVendor=SamsungCpuChip=S3C4510CpuEndian=LITTLEFlashVendor=SiliconStorageTechnologyFlashChip=SST39LF/VF160RamAddress=0x00400000FlashAddress=0x00000000FlashWidth=16FlashChipsPerSector=1LittleEndian=1Sim1=SYSCFG:$E7FFFF90       \Sim2=EXTDBWTH:003002   \Sim3=ROMCON0:000060&...
标签: 电脑入门
对于普通的不依赖于外置程序目录结构的注册表项目,可以用常规方法将修改项写成REG文件(可以直接从XP系统中导出,然后在其基础上修)。在外置程序目录下有个RegDoc.cmd,这个文件是毛桃用来注册外置程序文档关联的(就是改注册表),可以用记事本打开它,加入一行: 则PE加载外部配置时它会自动导入。 这是最简单的改注册表的方法。但是如果要修...
标签: windows 操作系统
说起硬件配置文件,有很多朋友都忽略了它在Windows XP中所起的作用,其实如果利用得当的话,可以大大提高系统的工作效率。 什么是硬件配置文件 所谓硬件配置文件,是指在启动计算机时告诉Windows应该启动哪些设备,以及使用每个设备中的哪些设置的一系列指令。当用户第一次安装Windows时,系统会自动创建一个名为“Profile 1...
  为了方便配置web.config文件,我写了一个常用的web.config文件的示例,可以以此为模版根据需要修改。 创建web.config文件的三种快捷方法:     1、用VS2005中的asp.net网站配置工具配置     2、参考C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727(.net framework版本)\CONFIG\目录下的machine.config 文...
当然不是,现在已经有一个新趋势,Java程序的配置文件都开始使用XML格式,以前是使用类似windows的INI格式(Java中也有Propertiesy这样的类专门处理这样的属性配置文件)。使用XML作为Java的配置文件有很多好处,从Tomcat的安装配置文件和J2ee的配置文件中,我们已经看到XML的普遍应用,让我们也跟随流行趋势用XML武装起来。 现在要害是...

经验教程

979

收藏

83
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部