import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class FormateBankAccountId {
List standardBankAccountIdFormat ;
public FormateBankAccountId(String propertiesFileName) throws FileNotFoundException, IOException
{
// 加载资源文件
Properties properties = new Properties();
properties.load(new FileReader(propertiesFileName));
this.standardBankAccountIdFormat = new ArrayList();
int keyNumber = 1;
String value = null;
// 读取键值对,键的格式是: formate_1 ,..., formate_10,...,formate_100 等
while( ( value = properties.getProperty("formate_" + keyNumber++) ) != null)
{
value = value.trim();
this.standardBankAccountIdFormat.add(value);
}
}
public List formate(String orginalBankAccountId)
{
List proceededlBankAccountIds = new ArrayList();
orginalBankAccountId = orginalBankAccountId.trim();
for(String formateString : this.standardBankAccountIdFormat)
{
// 格式字符串中的数字个数,
int formateStringDigitNum = formateString.replace("-", "").length();
//原字符串过长,截取后面部分
if(orginalBankAccountId.length() > formateStringDigitNum)
{
orginalBankAccountId = orginalBankAccountId.substring(orginalBankAccountId.length() - formateStringDigitNum);
}
//原字符串过短,前面补零
else if(orginalBankAccountId.length() < formateStringDigitNum)
{
String pre = "";
for(int i = 0 ; i < formateStringDigitNum - orginalBankAccountId.length() ; i ++) pre += "0";
orginalBankAccountId = pre + orginalBankAccountId;
}
// 格式化字符串中分隔符位置
int delimiterPosition = 0;
String temp = orginalBankAccountId;
while(-1 != (delimiterPosition = formateString.indexOf('-',delimiterPosition)))
{
// 添加分割符号
temp = temp.substring(0,delimiterPosition) + '-' + temp.substring(delimiterPosition );
delimiterPosition ++;
}
proceededlBankAccountIds.add(temp);
}
return proceededlBankAccountIds;
}
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
String orginalBankAccountId = "000111111222";
FormateBankAccountId formateTool = new FormateBankAccountId("bankAccountId.properties");
for(String proceededBankAccountId: formateTool.formate(orginalBankAccountId))
{
System.out.println(proceededBankAccountId);
}
}
}
/*
bankAccountId.properties:
formate_1 ###-######-###
formate_2 ##-####-###-##
formate_3 ####-#-###-###
formate_4 #-########-###
formate_5 ###-########-#
*/
/*
output :
000-111111-222
00-1111-112-22
0011-1-111-222
0-00111111-222
000-11111122-2
*/
取消
评论