国产探花免费观看_亚洲丰满少妇自慰呻吟_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垃圾回收機制

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阆中市| 苏州市| 泾川县| 哈尔滨市| 安顺市| 深泽县| 梨树县| 瓮安县| 库尔勒市| 繁昌县| 略阳县| 郓城县| 六枝特区| 云南省| 南平市| 大埔县| 衡东县| 宜昌市| 德清县| 勐海县| 洮南市| 景德镇市| 维西| 龙岩市| 安西县| 山西省| 类乌齐县| 凯里市| 博罗县| 长宁县| 康乐县| 苗栗县| 乌兰县| 白沙| 军事| 麟游县| 安徽省| 屏东县| 大埔区| 辛集市| 临沂市|