乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      怎么讀取war包中的properties文件,不使用web請(qǐng)求上下文的方式實(shí)現(xiàn)

       好閨女瑤瑤 2016-01-22
      當(dāng)前位置:我的異常網(wǎng)? Java相關(guān) ? 怎么讀取war包中的properties文件,不使用web請(qǐng)求上

      怎么讀取war包中的properties文件,不使用web請(qǐng)求上下文的方式實(shí)現(xiàn)

      www.MyException.Cn  網(wǎng)友分享于:2013-02-24  瀏覽:342次
      如何讀取war包中的properties文件,不使用web請(qǐng)求上下文的方式實(shí)現(xiàn)
      如何讀取war包中的properties文件,不使用web請(qǐng)求上下文的方式實(shí)現(xiàn),
      項(xiàng)目需求,我想在java project項(xiàng)目中,寫(xiě)個(gè)類可以讀取war包中的配置文件信息
      有什么好的辦法呀?

      請(qǐng)高手解惑,不勝感激?。?!

      ------解決方案--------------------
      Java code
      執(zhí)行前需要將sms.properties文件放在src目錄。。
      
      package com.test.impt;
      
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.util.Properties;
      
      public class ReadProperties {
      
          // 配置文件名稱
          private static final String CONFIG_PROPERTIES_FILE = "sms.properties";
          
          /**
           * @param args
           */
          public static void main(String[] args) {
              // TODO Auto-generated method stub
              String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
              try {
                  Properties props = loadProperties(filePath + CONFIG_PROPERTIES_FILE);
                  System.out.println(props.getProperty("dx.server.host")); // 成功輸出
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          public static Properties loadProperties(String propertiesFile)
                  throws Exception {
              Properties props = null;
              // propertiesFile = getPropertiesFile(propertiesFile);
      
              // ----------------------------
              // ...and if so, then load the values from that external file
              InputStream in = null;
              try {
                  in = new FileInputStream(propertiesFile);
                  props = new Properties();
                  props.load(in);
              } catch (Exception e) {
                  props = null;
              } finally {
                  if (in != null)
                      in.close();
              }
              return props;
          }
      }
      
      ------解決方案--------------------
      Java code
      還有一種方法。。代碼量更少的。。
      package com.test.impt;
      
      import java.io.FileInputStream;
      import java.io.InputStream;
      import java.util.Properties;
      
      public class ReadProperties {
      
          // 配置文件名稱
          private static final String CONFIG_PROPERTIES_FILE = "sms.properties";
      
          /**
           * @param args
           */
          public static void main(String[] args) {
              // TODO Auto-generated method stub
      
              InputStream is = ClassLoader.getSystemResourceAsStream(CONFIG_PROPERTIES_FILE);
              try {
                  Properties props = loadPropertiesFileStream(is);
                  System.out.println(props.getProperty("dx.server.host")); // 成功輸出
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
      
          public static Properties loadPropertiesFileStream(InputStream is)
                  throws Exception {
              Properties props = null;
              try {
                  props = new Properties();
                  props.load(is);
              } catch (Exception e) {
                  props = null;
              } finally {
                  if (is != null)
                      is.close();
              }
              return props;
          }
      }
      發(fā)布此文章僅為傳遞網(wǎng)友分享,不代表本站觀點(diǎn),若侵權(quán)請(qǐng)聯(lián)系我們刪除,本站將不對(duì)此承擔(dān)任何責(zé)任。

      文章評(píng)論

      社交賬號(hào)登錄:

      • 還沒(méi)有評(píng)論,沙發(fā)等你來(lái)?yè)?/li>

      我的異常網(wǎng)正在使用多說(shuō)

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多