properties文件操作类
可以使用java.util.Properties读取.properties文件中的内容
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.core.io.ClassPathResource;
/**properties配置文件读取*/
public class ConfigReader
{
// 文件路径
private static final String file = "sql.properties";
//配置保存类
private static final Properties props = new Properties();
static
{
ClassPathResource resource = new ClassPathResource(file);
InputStream inputStream = null;
try
{
inputStream = resource.getInputStream();
props.load(inputStream);
}
catch (Exception e)
{
//打印错误日志
}
finally
{
closeQuietly(inputStream);
}
}
/**
* 根据名字获取
*/
public static String getProperty(String propertyName)
{
return props.getProperty(propertyName);
}
/*
*资源关闭
*/
public static void closeQuietly(Closeable closeable)
{
if (null != closeable)
{
try
{
closeable.close();
}
catch (Exception e)
{
}
}
}
}
config.properties文件内容
name=zqq
age=18
使用
ConfigReader.getProperty("name");