java讀取properties檔案總結

來源:互聯網
上載者:User

標籤:位元組流   image   input   system   imp   技術分享   stat   cat   adt   

一、java讀取properties檔案總結

在java項目中,操作properties檔案是經常要做的,因為很多的配置資訊都會寫在properties檔案中,這裡主要是總結使用getResourceAsStream方法和InputStream流去讀取properties檔案,使用getResourceAsStream方法去讀取properties檔案時需要特別注意properties檔案路徑的寫法,測試專案如下:

 

 

/*    範例名稱:java讀取properties檔案總結 *     源檔案名稱:PropertiesFileReadTest.java *    要  點: *        1. 使用getResourceAsStream方法讀取properties檔案 *        2. 使用InPutStream流讀取properties檔案 *        3. 讀取properties檔案的路徑寫法問題 *         **/package propertiesFile.read.test;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.text.MessageFormat;import java.util.Properties;public class PropertiesFileReadTest {    public static void main(String[] args) throws FileNotFoundException {        readPropFileByGetResourceAsAtream();        System.out.println("--------------");        //readPropFileByInPutStream();    }    /*     * 使用getResourceAsAtream方法讀取     */    private static void readPropFileByGetResourceAsAtream() {        /*         * 讀取src下面config.properties包內的設定檔 test1.properties位於config.properties包內         */        InputStream inl = PropertiesFileReadTest.class.getClassLoader()                .getResourceAsStream("config/properties/test1.properties");        /*         * 讀取和PropertiesFileReadTest類位於同一個包裡面的設定檔         * test2.properties和PropertiesFileReadTest類在同一個包內         */        InputStream in2 = PropertiesFileReadTest.class.getResourceAsStream("test2.properties");        /*         * 讀取src根目錄下檔案的設定檔 jdbc.properties位於src目錄         */        InputStream in3 = PropertiesFileReadTest.class.getClassLoader().getResourceAsStream("jdbc.properties");        /*         * 讀取位於另一個source檔案夾裡面的設定檔 config是一個source檔案夾,config.properties位於config         * source檔案夾中         */        InputStream in4 = PropertiesFileReadTest.class.getClassLoader().getResourceAsStream("config.properties");        Properties properties = new Properties();        System.out.println("----使用getResourceAsStream方法讀取properties檔案----");        // 從輸入位元組流讀取屬性列表(鍵,值)        try {            System.out.println("-----------------------");            properties.load(inl);            System.out.println("test1.properties:name=" + properties.getProperty("name") + ",age="                    + properties.getProperty("age"));            System.out.println("-----------------------");            System.out.println("-----------------------");            properties.load(in2);            System.out.println("test2.properties:name=" + properties.getProperty("name") + ",age="                    + properties.getProperty("age"));            System.out.println("-----------------------");            properties.load(in3);            System.out.println("jdbc.properties:");            // 使用指定的格式字串和參數返回格式化的字串, 這裡的%s是java String預留位置            System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url")));            System.out.println(String.format("jdbc.usename=%s", properties.getProperty("jdbc.usename")));            System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password")));            properties.load(in4);            System.out.println("config.properties:");            // 使用給定的模式建立一個MessageFormat,並使用它來格式化給定的參數,{0}是一個java的字串預留位置            System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser")));            System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));            System.out.println(MessageFormat.format("database={0}", properties.getProperty("database")));            System.out.println("----------------------------------------------");        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            if(inl != null) {                try {                    inl.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(in2 != null) {                try {                    inl.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(in3 != null) {                try {                    inl.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(in4 != null) {                try {                    inl.close();                } catch (IOException e) {                    e.printStackTrace();                }            }                    }    }
/*     * 使用InputStream流讀取properties     */    private static void readPropFileByInPutStream() throws FileNotFoundException {        InputStream in1=null;        InputStream in2=null;        InputStream in3=null;        InputStream in4=null;        System.out.println("----使用InputStream流讀取properties檔案----");        try {        /*          * 讀取src下面config.properties包內的設定檔 test1.properties位於config.properties包內          */                in1 =new BufferedInputStream(new FileInputStream("src/config/properties/test1.properties"));        /*         * 讀取和PropertiesFileReadTest類位於同一個包裡面的設定檔          * test2.properties和PropertiesFileReadTest類在同一個包裡面         */         in2=new BufferedInputStream(new FileInputStream("src/propertiesFile/read/test/test2.properties"));        /*         * 讀取src根目錄下檔案的設定檔          * jdbc.properties位於src目錄         */         in3 = new BufferedInputStream(new FileInputStream("src/jdbc.properties"));        /*         * 讀取位於另一個source檔案夾裡面的設定檔          * config是一個source檔案夾,config.properties位於config source檔案夾中         */        in4 = new FileInputStream("config/config.properties");                Properties properties=new Properties();                    System.out.println("-----------------------");            properties.load(in1);            System.out.println("test1.properties:name=" + properties.getProperty("name") + ",age="                    + properties.getProperty("age"));            System.out.println("-----------------------");            System.out.println("-----------------------");            properties.load(in2);            System.out.println("test2.properties:name=" + properties.getProperty("name") + ",age="                    + properties.getProperty("age"));            System.out.println("-----------------------");            properties.load(in3);            System.out.println("jdbc.properties:");            // 使用指定的格式字串和參數返回格式化的字串, 這裡的%s是java String預留位置            System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url")));            System.out.println(String.format("jdbc.usename=%s", properties.getProperty("jdbc.usename")));            System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password")));            properties.load(in4);            System.out.println("config.properties:");            // 使用給定的模式建立一個MessageFormat,並使用它來格式化給定的參數,{0}是一個java的字串預留位置            System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser")));            System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));            System.out.println(MessageFormat.format("database={0}", properties.getProperty("database")));            System.out.println("----------------------------------------------");                                } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally {            if (in1 != null) {                try {                    in1.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (in2 != null) {                try {                    in2.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (in3 != null) {                try {                    in3.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (in4 != null) {                try {                    in4.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }

 

java讀取properties檔案總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.