java读取properties文件总结

一、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();
                }
            }
        }

    }

 

posted @ 2018-08-23 14:18  言西早石头侠  阅读(283)  评论(0编辑  收藏  举报