一.概述:
在实际的项目中大多数的数据都是存放在数据库中的,但是有一些用于配置的信息大多存放在以properties文件中,比如我们常用的log4j.properties文件就是用来配置日志信息的,db.propeties文件就是用来配置数据库信息的。除了一些框架自带的properties文件之外根据项目的需要往往还要自定义properties文件存放一些跟业务相关的配置,比如文件上传路径,某个接口的调用地址等等。为了读写方便java语言提供了java.util.Properties类来操作.properties文件。
二.编程示例:
1.properties文件格式
properties文件中存放的内容全部为键值对(key=value)开头加#号表示注释,下面是一个简单properties配置文件
application.properties
#this is a property file
#database
db.username=jwang
db.password=jwang
#file upload path
file.upload.video=/home/jwang/video
file.upload.image=/home/jwang/image
2.properties文件读写工具类
(1)PorpertiesUtil.java
package com.jwang.common.utils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
/**
* @author jwang
* properties文件读写工具类
*
*/
public abstract class PropertiesUtil
{
private static final Logger LOG = Logger.getLogger(PropertiesUtil.class);
public static Properties getProperties(String path)
{
Properties prop = new Properties();
loadProp(prop, path);
return prop;
}
private static void loadProp(Properties p, String conf)
{
InputStream is = getInputStream(conf);
if(null != is)
{
try
{
p.load(is);
}
catch (IOException e)
{
LOG.info("file not found!");
}
finally
{
if(is != null)
{
try
{
is.close();
}
catch (IOException e)
{
LOG.info("stream close fail!");
}
}
}
}
}
//获取输入流
private static InputStream getInputStream(String conf)
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream is = null;
if(null != classLoader)
{
is = classLoader.getResourceAsStream(conf);
}
return is;
}
//获取输出流
private static OutputStream getOutPutStream(String conf)
{
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
OutputStream out = null;
if(null != classLoader)
{
String filePath = classLoader.getResource(conf).getFile();
try
{
out = new FileOutputStream(filePath);
}
catch (FileNotFoundException e)
{
LOG.info("file not found!!!");
}
}
return out;
}
//根据key读取value
public static String getValue(Properties p, String key)
{
String value = p.getProperty(key);
return value == null?"":value;
}
//设置key=value
public static void setValue(String conf, String key, String value)
{
Properties p = getProperties(conf);
OutputStream out = getOutPutStream(conf);
p.setProperty(key, value);
try
{
p.store(out, "set:"+key+"="+value);
}
catch (IOException e)
{
LOG.info("set properties fail!!!");
}
finally
{
if(out != null)
{
try
{
out.close();
}
catch (IOException e)
{
LOG.info("stream close fail!");
}
}
}
}
}
(2)测试类:PropertiesTest.java
package com.jwang.test;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.junit.Test;
import com.jwang.common.utils.PropertiesUtil;
/**
* @author jwang
* 简单的properties文件读写测试类
*
*/
public class PropertiesTest
{
private static final Logger LOG = Logger.getLogger(PropertiesTest.class);
private static final String PROPETIES_PATH = "conf/application.properties";
private static final Properties PROP = PropertiesUtil.getProperties(PROPETIES_PATH);
private static final String UPLOAD_PDF_PATH = "/home/jwang/pdf";
@Test
public void testGetProperties()
{
String userName = PROP.getProperty("db.username");
String passWord = PROP.getProperty("db.password");
LOG.info("userName:"+userName);
LOG.info("passWord:"+passWord);
PropertiesUtil.setValue(PROPETIES_PATH, "file.upload.pdf", UPLOAD_PDF_PATH);
}
}
(3)测试结果
测试前application.properties文件内容
测试后application.properties文件的内容
控制台输出如下所示
三.扩展总结(待补充)
1.Properties类的其他方法
2.项目中获取文件路径的方法