WEB-INF/classes/number.properties 配置文件:
#还款期限45天
repay_deadline=45
java读取配置文件:
package com.tcwl.vsmp.utils;
import java.io.*;
import java.util.Properties;
/**
* Commons Configuration读取属性文件的例子
*
*/
public class BatchNumber
{
/**
*
*/
private static String configPath = BatchNumber.class.getResource(
"/number.properties").getFile();
/**
*
*/
private static String numAddPath = BatchNumber.class.getResource(
"/numAdd.properties").getFile();
public synchronized static void setTerminateNm(){
Properties prop = new Properties();
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(
numAddPath));
prop.load(in); // /加载属性列表
prop.setProperty("serialNum", ""+1);
FileOutputStream outputFile = new FileOutputStream(numAddPath);
prop.store(outputFile, "modify");
outputFile.flush();
outputFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到一个字符串,是一个递增的数字,每调用一次增加1
*/
public synchronized static String getNumberValue(String key)
{
Properties prop = new Properties();
String str = "";
//String str1 = "";
try
{
// 读取属性文件a.properties
InputStream in = new BufferedInputStream(new FileInputStream(
numAddPath));
prop.load(in); // /加载属性列表
//Integer num = Integer.valueOf(prop.getProperty("numadd"));
Integer num = Integer.valueOf(prop.getProperty(key));
if(key.equals("numadd")){
str = String.format("%08d", num);
num = (num >= 99999999) ? 1 : num + 1;
}else{
str = String.valueOf(num);
num = num + 1;
}
System.out.println(str);
//in.
in.close();
//prop.setProperty("numadd", ""+num);
prop.setProperty(key, ""+num);
FileOutputStream outputFile = new FileOutputStream(numAddPath);
prop.store(outputFile, "modify");
outputFile.flush();
outputFile.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
}
return str;
}
/**
*
* @param key key
* @return 结果
*/
public static String getValue(String key)
{
Properties prop = new Properties();
String value = "";
try
{
// 读取属性文件a.properties
InputStream in = new BufferedInputStream(new FileInputStream(
configPath));
prop.load(in); // /加载属性列表
value = prop.getProperty(key);
in.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println(e);
}
return value;
}
/**
*
* @param args 参数
*/
public static void main(String[] args)
{
// // 当前类的绝对路径
// String cp = "/number.properties";
// System.out.println(BatchNumber.class.getResource("/").getFile());
// // 指定CLASSPATH文件的绝对路径
//
// System.out.println(BatchNumber.class.getResource(cp).getFile());
// // 指定CLASSPATH文件的绝对路径
//
// File f = new File(BatchNumber.class.getResource(cp).getFile());
// System.out.println(f.getPath());
// System.out.println(BatchNumber.getValue());
}
/**
* 设置某个key的值,并保存至文件.
* @param key key值
* @param value 值
* @throws IOException
*/
public void setValue(String key, String value) throws IOException
{
Properties prop = new Properties();
InputStream fis = new FileInputStream(this.configPath);
// 从输入流中读取属性列表(键和元素对)
prop.load(fis);
// System.out.println(prop.get(key));
// 调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(this.configPath);
prop.setProperty(key, value);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "last update");
// 关闭文件
fis.close();
fos.close();
}
}
java定义常量:
package com.tcwl.vsmp.common;
/**
* OilConstant
*
*/
public class OilConstant
{
/**
* 还款期限40天
*/
public static final String REPAY_DEADLINE = "repay_deadline";
}
通过常量数据 获取配置文件数据:
/**
* 还款期限45天
*/
Integer REPAY_DEADLINE =Integer.valueOf(BatchNumber
.getValue(OilConstant.REPAY_DEADLINE));