• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    java解析cfg.properties屬性文件

           properties是一種屬性文件,這種文件以key=value格局存儲內容。Java中可以利用java.util.Properties類來讀取這個文件,按照左邊的key獲取值,String value=p.getProperty(key);

           多種解析屬性文件的體例中java.util.Properties解析文件的體例是同一的,都是經由過程load加載InputStream 對象。本家兒如果獲取InputStream 對象的體例分歧,現實利用中我們紛歧心猿意馬能精確獲取properties屬性文件的絕對路徑。

          屬性文件在eclipse和IDEA沒有安裝插件的環境下展示時會將中文轉為Unicode編碼,eclipse選中內容F2會顯示具體的中文內容(如下圖)。

    東西/原料

    • 電腦
    • eclipse或者intellij IDEA

    第一步調:properties屬性文件解析體例

    1. 1

      第一種:本家兒如果經由過程class.getClassLoader().getResourceAsStream

      1、是實現獲取在classpath路徑下的資本文件的輸入流

      為什么是classpath而不是src,因為當web項目運行時,IDE編譯器會把src下的一些資本文件移至WEB-INF/classes,classPath目次其實就是這個classes目次。這個目次下放的一般是web項目運行時的class文件、資本文件(xml,properties...);

      2、本家兒要代碼:

      import java.io.IOException;

      import java.io.InputStream;

      import java.util.Properties;

      public class PropertiesUtils {

      /**

      * 按照key獲取cfg.properties的值

      * @param key

      * @return

      */

      public static String getCfgPropertiesValue(String key) {

      Properties pro = new Properties();

      InputStream is = null;

      try {

      is = PropertiesUtils.class.getClassLoader().getResourceAsStream(

      "CodeMapping.properties");

      System.out.println();

      // 讀取屬性文件

      pro.load(is);

      } catch (IOException e) {

      e.printStackTrace();

      } finally {

      try {

      if (is != null) {

      is.close();

      }

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

      return pro.getProperty(key);

      }

      }

      3、main方式測試成果

      public class Test5 {

      public static void main(String[] args) {

      System.out.println("name:"

      + PropertiesUtils.getCfgPropertiesValue("name"));

      }

      }

    2. 2

      第二種:經由過程class.getResourceAsStream(String name)獲取。

      import java.io.IOException;

      import java.io.InputStream;

      import java.util.Properties;

      public class Test5 {

      public static void main(String[] args) {

      System.out.println("name:" + getCfgPropertiesValue("name"));

      }

      /**

      * 按照key獲取CodeMapping.properties的值

      * @param key

      * @return

      */

      public static String getCfgPropertiesValue(String key) {

      Properties pro = new Properties();

      InputStream is = null;

      try {

      is = Test5.class

      .getResourceAsStream("/CodeMapping.properties");

      System.out.println();

      // 讀取屬性文件

      pro.load(is);

      } catch (IOException e) {

      e.printStackTrace();

      } finally {

      try {

      if (is != null) {

      is.close();

      }

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

      return pro.getProperty(key);

      }

      }

    3. 3

      第三種:利用絕對路徑解析。

      1、這種利用絕對路徑的體例需要你可以或許精確供給位置

      2具體代碼如下所示:

      import java.io.FileInputStream;

      import java.io.IOException;

      import java.io.InputStream;

      import java.util.Properties;

      public class Test5 {

      public static void main(String[] args) {

      System.out

      .println("name:"

      + getPropertiesValue(

      "D:/Workspaces/MyEclipse 10/servlet/src/CodeMapping.properties",

      "name"));

      }

      public static String getPropertiesValue(String filePath, String key) {

      Properties pro = new Properties();

      InputStream in = null;

      try {

      // 讀取屬性文件

      in = new FileInputStream(filePath);

      pro.load(in);

      } catch (IOException e) {

      e.printStackTrace();

      } finally {

      try {

      in.close();

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

      return pro.getProperty(key);

      }

      }

    第二步調:常識總結

    1. 1

      第一步:javaweb項目打包總結。

      1、通俗javaweb項目中的編譯會將內容編譯在WEB-INF\classes

      1、springboot項目當地開辟會將src/main/java和resources目次下的內容編譯到target\classes下。打當作jar包之后:excelimport-0.0.1-SNAPSHOT.jar。目次為:excelimport-0.0.1-SNAPSHOT.jar\BOOT-INF\classes

    2. 2

      第二步:class.getClassLoader().getResourceAsStream(file)和class.getResourceAsStream(file)比力。

      1、都是實現獲取在classpath路徑下的資本文件的輸入流。

      2、為什么是classpath而不是src,因為當web項目運行時,IDE編譯器會把src下的一些資本文件移至WEB-INF/classes,classPath目次其實就是這個classes目次。這個目次下放的一般是web項目運行時的class文件、資本文件(xml,properties...);

      3、class.getClassLoader().getResourceAsStream(file)半斤八兩于直接在根目次classes查找文件和Thread.currentThread().getContextClassLoader().getResourceAsStream一樣,文件不在根目次classes下無法讀取。

      InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("CodeMapping.properties");

      4、class.getResourceAsStream(file)定位相對于文件夾classes獲取下一級內容需要加“/”到具體目次

      Test5.class.getResourceAsStream("/CodeMapping.properties");

    注重事項

    • 解析properties屬性文件不需要添加依靠包利用jdk自帶的類就可以解決
    • 發表于 2019-05-08 20:02
    • 閱讀 ( 702 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆