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一米陽光 閱讀(4936) 評論(0)  編輯  收藏 所屬分類: 總結備用
          主站蜘蛛池模板: 渝北区| 浏阳市| 桂平市| 兴宁市| 鄂尔多斯市| 会宁县| 田阳县| 枝江市| 凭祥市| 聊城市| 商水县| 新巴尔虎右旗| 鹤壁市| 南昌县| 崇明县| 科尔| 临澧县| 乳源| 宁津县| 新宾| 新密市| 德钦县| 游戏| 凤凰县| 西畴县| 洪泽县| 莱州市| 梅州市| 大兴区| 北票市| 开封市| 济源市| 张家界市| 咸阳市| 额敏县| 剑川县| 永善县| 建始县| 仙游县| 廊坊市| 越西县|