<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    waysun一路陽光

    不輕易服輸,不輕言放棄.--心是夢的舞臺,心有多大,舞臺有多大。踏踏實實做事,認認真真做人。

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
      167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks

    Configuration.java
    來源于網絡,本人進行了簡單的修改,版權歸屬原作者,特此聲明。

    /**
    *
    *<p>Blog:http://www.cnweblog.com/nm1504</p>
    *<p>E-mail:yyk1504@163.com</p>
    *<p>創建時間:2008-3-13-下午03:11:32</p>
    *<p>Copyright: (c)2008-3-13</p>
    */
    package cn.com.mfsoft.config;

    import java.io.*;
    import java.net.URL;
    import java.net.URLDecoder;
    import java.util.*;

    import org.apache.log4j.Logger;


    import cn.com.mfsoft.config.ConfigurationException;

    public class Configuration
    {
      private Properties config=new Properties();//記錄配置項
      private String fn=null;//記錄配置文件名
      //此構造方法用于新建配置文件
      public Configuration()
      {

       String path="";
         try
         {
           //File file = new File("system.conf");
             URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf");
             path = u.getPath();
              //獲得用戶工程目錄System.getProperty("user.dir")
              //path=(System.getProperty("user.dir").replace("\\", "/"))+"/model/Model_"+model_id+".snet";
        // path=URLDecoder.decode(path, "utf-8");

                    System.out.println("11"+path);
                    path=URLDecoder.decode(path, "utf-8");
                    System.out.println("22"+path);
             FileInputStream fin = new FileInputStream(path);
             config.load(fin); //載入文件
             fin.close();
         }
         catch (IOException ex)
         {
             try
             {
        throw new ConfigurationException
          ("無法讀取指定的配置文件:"+path);
       } catch (ConfigurationException e)
       {
        e.printStackTrace();
       }
         }
         fn=path;
      }
     
      public String getPrjRoot()
      {
          URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf");
          String str = u.getPath();
          if (str.indexOf(":") != -1)
              str = str.substring(1);// 在windows下面為/F:/MyPrj/WORK/post
          else
              str = str.substring(0);// 在Linux下面為/usr/local/apache...
          return str;
      }

      //從指定文件名讀入配置信息
      public Configuration(String fileName)throws ConfigurationException
      {
        try
        {
            FileInputStream fin = new FileInputStream(fileName);
            config.load(fin); //載入文件
            fin.close();
        }
        catch (IOException ex)
        {
             throw new ConfigurationException
              ("無法讀取指定的配置文件:"+fileName);
        }
        fn=fileName;
      }

     

      //指定配置項名稱,返回配置值
      public String getValue(String itemName)
      {
       String value=getValue("language","chinese");
       String st="";

       if(value.equals("chinese"))
       {

      st=toGb(config.getProperty(itemName));
       }
       else
       {
        st=config.getProperty(itemName);
       }
          return st;//config.getProperty(itemName);
      }

      public  String getValue2(String ItemName)
      {
       String value="";
      try
      {
        String path="";
        path=getClass().getResource("/system.conf").getPath().substring(1);
        path=URLDecoder.decode(path, "utf-8");
        System.out.println();
        Configuration config= new Configuration(path);
        value=config.getValue(ItemName);
      } catch (Exception e)
      {
       e.printStackTrace();
      }
        return value;
      }

      //指定配置項名稱和默認值,返回配置值
      public String getValue(String itemName,
                             String defaultValue)
      {
        return config.getProperty(itemName,defaultValue);
      }

      //設置配置項名稱及其值
      public void setValue(String itemName,String value){
        config.setProperty(itemName,value);
        return;
      }

      //保存配置文件,指定文件名和抬頭描述
      public void saveFile(String fileName,String description)throws ConfigurationException
      {
        try {
          FileOutputStream fout
              = new FileOutputStream(fileName);
          config.store(fout, description);//保存文件
          fout.close();
        }
        catch (IOException ex) {
          throw new ConfigurationException
              ("無法保存指定的配置文件:"+fileName);
        }
      }

      //保存配置文件,指定文件名
      public void saveFile(String fileName)throws ConfigurationException
      {
        saveFile(fileName,"");
      }

      //保存配置文件,采用原文件名
      public void saveFile() throws ConfigurationException {
        if(fn.length()==0)
          throw new ConfigurationException
              ("需指定保存的配置文件名");
        saveFile(fn);
      }

      public static String toGb(String uniStr)
      {
          String gbStr = "";
          if(uniStr == null)
          {
             uniStr = "";
          }
          try
          {
           byte[] tempByte = uniStr.getBytes("ISO8859_1");
           gbStr = new String(tempByte,"GB2312");
          }
          catch(Exception ex)
          {
          }
          return gbStr;
      }
      public static void main(String[]args)
      {
      }
    }


    ConfigurationException.java

    /**
    *
    *<p>Blog:http://www.cnweblog.com/nm1504</p>
    *<p>E-mail:yyk1504@163.com</p>
    *<p>創建時間:2008-3-13-下午03:12:29</p>
    *<p>Copyright: (c)2008-3-13</p>
    */
    package cn.com.mfsoft.config;

    public class ConfigurationException extends Exception
    {
       public ConfigurationException()
       {
       
       }
       public ConfigurationException(String msg)
       {
            super(msg);
       }
     }


    ReadConfig.java

    /**
    **<p>Blog:http://www.cnweblog.com/nm1504</p>
    *<p>E-mail:yyk1504@163.com</p>
    *<p>創建時間:2008-3-13-下午03:14:24</p>
    *<p>Copyright:  (c)2008-3-13</p>
    */
    package cn.com.mfsoft.config;

    import java.net.URL;


    public class ReadConfig
    {
      public static void main(String[] args)
      {
        try
        {
         Configuration config=new Configuration();
          //讀取指定文件
         ReadConfig rc=new ReadConfig();
        
            System.out.println("::::::;"+rc.getPrjRoot());
      
         try{
     
     /*
             System.out.println(new String(config.getValue("Max_Users_Count")));//5

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes(),"GB2312"));//6

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes(),"ISO8859_1"));//7

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312")));//8

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312"),"GB2312"));//9

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes("GB2312"),"ISO8859_1"));//10

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes("UTF-8")));//11

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes("UTF-8"),"GB2312"));//12

             System.out.println(new String(config.getValue("Max_Users_Count").getBytes("ISO8859_1"),"UTF-8"));//13
            
             System.out.println(toGb(config.getValue("Max_Users_Count")));*/

           }

           catch(Exception e)
           {
                 e.printStackTrace();
           }
        }
        catch(Exception ex)
        {
              ex.printStackTrace();
        }
       
       
      }
     


      public String getPrjRoot()
      {
          URL u = this.getClass().getResource(this.getClass().getSimpleName()+".conf");
          String str = u.getPath();
          if (str.indexOf(":") != -1)
              str = str.substring(1);// 在windows下面為/F:/MyPrj/WORK/post
          else
              str = str.substring(0);// 在Linux下面為/usr/local/apache...
          return str;
      }

     
    }


    SetConfig.java

    /**
    *
    *<p>Blog:http://www.cnweblog.com/nm1504</p>
    *<p>E-mail:yyk1504@163.com</p>
    *<p>創建時間:2008-3-13-下午03:13:20</p>
    *<p>Copyright:  (c)2008-3-13</p>
    */
    package cn.com.mfsoft.config;

    import cn.com.mfsoft.config.ConfigurationException;//包含這個包方能使用配置類

    public class SetConfig {
      public static void main(String[] args) {
        try
        {
          Configuration config = new Configuration();
          //設置一些屬性值
          config.setValue("language", "chinese");
          config.setValue("Max_Users_Count", "50");
          config.setValue("Max_OpenedFile_Count", "38");
          //保存文件
          config.saveFile("d:/system.conf",
                          "Sytem Global Configuration");
        }
        catch (ConfigurationException ex) {
          //捕獲我們自定義的異常
          ex.printStackTrace();
        }
      }
    }


     

    posted on 2008-06-20 08:45 weesun一米陽光 閱讀(4933) 評論(0)  編輯  收藏 所屬分類: 總結備用
    主站蜘蛛池模板: 国产自国产自愉自愉免费24区| 四虎永久在线精品免费观看地址| 黄色网址在线免费观看| 亚洲精品午夜视频| 国产亚洲大尺度无码无码专线 | 亚洲色成人中文字幕网站| 免费高清在线爱做视频| 亚洲黄色免费电影| 人妻在线日韩免费视频| 一级毛片aa高清免费观看| 亚洲国产精品日韩av不卡在线| 自怕偷自怕亚洲精品| 国产AV无码专区亚洲AV毛网站 | 国产成人综合亚洲| 亚洲综合精品伊人久久| 亚洲激情视频网站| 亚洲精品电影天堂网| 亚洲精品无码专区在线在线播放| 亚洲片国产一区一级在线观看 | 一级女性全黄久久生活片免费| 亚洲老熟女五十路老熟女bbw| 亚洲字幕在线观看| 亚洲欧洲日韩综合| 亚洲精品在线播放| 亚洲人成电影福利在线播放| 久久国产亚洲电影天堂| 亚洲精品无码av人在线观看| 亚洲香蕉成人AV网站在线观看 | 国产又黄又爽胸又大免费视频| 日韩一级片免费观看| 免费人成视频在线播放| 国产成人亚洲综合a∨| 老子影院午夜伦不卡亚洲| 亚洲国产精品嫩草影院| 羞羞漫画登录页面免费| 免费国产黄网站在线看| selaoban在线视频免费精品| 福利免费在线观看| 久艹视频在线免费观看| 亚洲黄色免费在线观看| 成人免费毛片视频|