자바 가 properties 파일 을 읽 는 세 가지 방식

6315 단어 자바properties
더 읽 기
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesTest
{
    /**
     *   key  value(      )
     * 
     * @param filePath
     *                , properties    classpath   ,
     *              :configure.properties  com.test.configure ,
     *                com/test/configure/configure.properties
     * @param key
     * @return
     */
    public static String getProperty(String filePath, String key)
    {
        String value = null;
        java.util.Properties props;
        try
        {
            props = PropertiesLoaderUtils.loadAllProperties(filePath);
            value = props.getProperty(key);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return value;

    }

    /**
     *   properties     (      )
     * 
     * @param filePath
     *                , properties    classpath   ,
     *              :configure.properties  com.test.configure ,
     *                com/test/configure/configure.properties
     */
    public static void getProperties(String filePath)
    {
        java.util.Properties props;
        try
        {
            props = PropertiesLoaderUtils.loadAllProperties(filePath);
            printAllProperty(props);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    /**
     *   key  value(      )
     * 
     * @param filePath
     *                     ,        ,              ,
     *             :    /configure/configure.properties,
     *                  configure/configure.properties
     * @param key
     * @return
     */
    public static String readValue(String filePath, String key)
    {
        Properties props = new Properties();
        String value = null;
        try
        {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            value = props.getProperty(key);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return value;
    }

    /**
     *   properties     (      )
     * 
     * @param filePath
     *                     ,        ,              , 
     *             :    /configure/configure.properties,
     *                  configure/configure.properties
     */
    public static void readProperties(String filePath)
    {
        Properties props = new Properties();
        try
        {
            InputStream in = new BufferedInputStream(new FileInputStream(filePath));
            props.load(in);
            printAllProperty(props);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     *   key  value(      )
     * 
     * @param filePath
     *                , properties    classpath   ,
     *              :configure.properties  com.test.configure ,
     *                /com/test/configure/configure.properties
     * @param key
     * @return
     */
    public static String getValue(String filePath, String key)
    {
        Properties props = new Properties();
        try
        {
            //
            InputStream in = Object.class.getResourceAsStream(filePath);
            props.load(in);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return props.getProperty(key);
    }

    /**
     *   properties     (      )
     * 
     * @param filePath
     *                , properties    classpath   ,
     *              :configure.properties  com.test.configure ,
     *                /com/test/configure/configure.properties
     * @return
     */
    public static void getValues(String filePath)
    {
        Properties props = new Properties();
        try
        {
            InputStream in = Object.class.getResourceAsStream(filePath);
            props.load(in);
            printAllProperty(props);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void printAllProperty(java.util.Properties props)
    {
        @SuppressWarnings("rawtypes")
        Enumeration en = props.propertyNames();
        while (en.hasMoreElements())
        {
            String key = (String) en.nextElement();
            String value = props.getProperty(key);
            System.out.println(key + value);
        }
    }

    public static void main(String[] args)
    {
        System.out.println(getProperty("com/test/configure/configure.properties", "jdbc.driverClassName"));
        getProperties("com/test/configure/configure.properties");
        System.out.println(readValue("configure/configure.properties", "jdbc.driverClassName"));
        readProperties("configure/configure.properties");
        System.out.println(getValue("/com/test/configure/configure.properties", "jdbc.driverClassName"));
        getValues("/com/test/configure/configure.properties");
    }
}

 
  
   전재 출처 를 밝 혀 주 십시오:http://xieke90.iteye.com/blog/2233046
 
  • GetProperties.zip (389.6 KB)

  • 다운로드 횟수:11

    좋은 웹페이지 즐겨찾기