자바 가 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
다운로드 횟수:11
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.