国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java讀取Properties文件工具類

2019-11-06 06:04:04
字體:
來源:轉載
供稿:網友

java.util.PRoperties工具類

作用:對屬性文件的簡單讀寫

在項目中用到了很對時間的對比,但是我又不想把它寫死,所以一直在找一種比較簡潔的使用配置文件來代替代碼中的參數,好了,上代碼了。

package cn.com.plustv.utils;import java.io.FileInputStream;import java.io.InputStream;import java.util.Properties;import org.apache.log4j.Logger;import cn.com.plustv.InteractionCode;public class PropertiesUtils {            private static Properties properties=new Properties();      private static Logger logger = Logger.getLogger(PropertiesUtils.class);  //log4j日志輸出的方法    static{          try {              InputStream fis = new FileInputStream(System.getProperty("user.dir") +"/conf/env.properties");            //注意屬性配置文件所在的路徑 ,因為我的程序要打jar包,所以我在項目下建立了conf文件夾,此文件夾和src同級,故需要System.getProperty("user.dir")來進行拼接。

            //如果你的項目不需要,直接采用properties.load(PropertiesUtils.class.getClassLoader().getResourceAsStream("./properties/env.properties")); 就可以了,而     

            //properties是包名,就是直接將env.properties放在properties(自己新建)這個包下。            properties.load(fis);          } catch (Exception e) {              logger.error(e.getMessage());  //此處是我項目中采用dom4j來進行日志輸出        }      }            //讀取屬性配置文件中的某個屬性對應的值      public static String readProperty(String property){          return (String) properties.get(property);      }            } 

上面是PropertiesUtils類,我自己放在一個工具包中,如下圖所示:

下面是env.properties文件的內容,如下圖所示:

因為在項目中我需要的int值,所以需要進行以下轉換

package cn.com.plustv.utils;public class Test {    private static int uploadTime;    private static int onlineTime;    private static int offlineTime;    private static int TimerTime;    private static int timeinterval;    static {        String imageupload1 = "imageupload";        String onlineimage1 = "onlineimage";        String offlineimage1 = "offlineimage";        String timer = "timer";        String timeintervalStr = "timeinterval";        String uploadValue = PropertiesUtils.readProperty(imageupload1);        String onlineValue = PropertiesUtils.readProperty(onlineimage1);        String offlineValue = PropertiesUtils.readProperty(offlineimage1);        String timertimer = PropertiesUtils.readProperty(timer);        String timeintervalValue = PropertiesUtils.readProperty(timeintervalStr);        uploadTime = Integer.parseInt(uploadValue);//在此時我進行了轉換        onlineTime = Integer.parseInt(onlineValue);        offlineTime = Integer.parseInt(offlineValue);        TimerTime = Integer.parseInt(timertimer);        timeinterval = Integer.parseInt(timeintervalValue);    }    public static void main(String[] args) {        System.out.println("imageupload的時間是"+uploadTime);        System.out.println("onlineimage的時間是"+onlineTime);        System.out.println("offlineimage的時間是"+offlineTime);        System.out.println("timer的時間是"+TimerTime);        System.out.println("timeinterval的時間是"+timeinterval);    }}

最后運行得到以下結果:


上一篇:SQLite - Java

下一篇:Java垃圾回收機制

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宣汉县| 乌苏市| 花垣县| 高邑县| 呼玛县| 迁西县| 仁寿县| 永德县| 石林| 沁阳市| 乳源| 阳山县| 中山市| 余干县| 陵川县| 峨边| 日土县| 宜都市| 长白| 文登市| 宜兰县| 清丰县| 额济纳旗| 长春市| 合作市| 桦南县| 乌苏市| 汨罗市| 左贡县| 措勤县| 江永县| 湖南省| 汾西县| 仁化县| 钟山县| 乐亭县| 长乐市| 潢川县| 洪湖市| 嘉兴市| 沂水县|