posts - 22, comments - 32, trackbacks - 0, articles - 73
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          首先在web工程src目錄下新建一個database.properties 文件
          內容如下:

          user=root
          password=root
          databaseType=com.mysql.jdbc.Driver
          url=jdbc:mysql://192.168.2.232:3306/oa? seUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull

           這里的內容隨自己的合適而變化,這里不多說了;

          在新建一個讀取.properties文件新類:

          package com.junhai.tamsys.util;

          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.util.Properties;

          public class DatabaseConfigure {
           private Properties property;
           private FileInputStream inputFile;
           private FileOutputStream outputFile;

           public DatabaseConfigure() {
            property = new Properties();
           }

           public DatabaseConfigure(String filePath) {
            property = new Properties();
            try {
             inputFile = new FileInputStream(filePath);
             property.load(inputFile);
             inputFile.close();
            } catch (FileNotFoundException e) {
             System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");
             e.printStackTrace();
            } catch (IOException e) {
             e.printStackTrace();
            }
           }

           /*
            * 重載函數,得到key的值 @param key 取得其值的鍵 @return key的值
            */
           public String getValue(String key) {
            if (property.containsKey(key)) {
             return property.getProperty(key);

            } else
             return "";
           }

           /*
            * 重載函數,得到key的值
            *
            * @param fileName propertys文件的路徑+文件名 @param key 取得其值的鍵 @return key的值
            */
           public String getValue(String fileName, String key) {
            try {
             String value = "";
             inputFile = new FileInputStream(fileName);
             property.load(inputFile);
             inputFile.close();
             if (property.containsKey(key)) {
              value = property.getProperty(key);
              return value;
             } else
              return value;
            } catch (FileNotFoundException e) {
             e.printStackTrace();
             return "";
            } catch (IOException e) {
             e.printStackTrace();
             return "";
            } catch (Exception ex) {
             ex.printStackTrace();
             return "";
            }
           }

           /*
            * 清除properties文件中所有的key和其值
            */
           public void clear() {
            property.clear();
           }

           /*
            * 改變或添加一個key的值,當key存在于properties文件中時該key的值被value所代替, 當key不存在時,該key的值是value
            * @param key 要存入的鍵 @param value 要存入的值
            */
           public void setValue(String key, String value) {
            property.setProperty(key, value);
           }

           /*
            * 將更改后的文件數據存入指定的文件中,該文件可以事先不存在。 @param fileName 文件路徑+文件名稱 @param
            * description 對該文件的描述
            */
           public void saveFile(String fileName, String description) {
            try {
             outputFile = new FileOutputStream(fileName);
             property.store(outputFile, description);
             outputFile.close();
            } catch (FileNotFoundException e) {
             e.printStackTrace();
            } catch (IOException ioe) {
             ioe.printStackTrace();
            }
           }

           public static void main(String[] args) {
            DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");
            System.out.println(test.getValue("user"));
            System.out.println(test.getValue("databaseType")+";"+test.getValue("url"));
            
           }
          }


          這樣就可以通過key得到相應的value了;
          想在這里多說一點是路徑問題,java工程和web 工程讀取.properties路徑是不一樣的,我在這里就花了不少時間。
          JAVA工程: DatabaseConfigure test=new DatabaseConfigure("./src/database.properties");這樣讀取就可以了:
          web工程這樣讀取:DatabaseConfigure  dc = new DatabaseConfigure(Thread.currentThread().getContextClassLoader()
                                                          .getResource("").getPath()+"database.properties");
           
          因為當服務器啟動后工程里面東西會編譯后加到\WEB-INF\classes這個目錄,服務也是從這個目錄下讀取信息的。所以先取到這個路徑,才能正確讀取到database.properties這里邊的內容。


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 静宁县| 陇川县| 晋宁县| 元阳县| 大姚县| 兴宁市| 银川市| 德化县| 奉新县| 禄丰县| 秦安县| 体育| 柏乡县| 洮南市| 平陆县| 镇安县| 筠连县| 东安县| 老河口市| 会宁县| 江油市| 湖州市| 辽宁省| 德昌县| 安徽省| 兴仁县| 新蔡县| 九龙城区| 天峨县| 彩票| 都昌县| 定日县| 五家渠市| 龙岩市| 九龙坡区| 通江县| 岳阳市| 贵州省| 襄城县| 茂名市| 龙南县|