按规则生产txt文本

//生成保险公司数据
public static void main(String[] args){
int length = 900;
Random rd = new Random();
String[] xing = new String[] { "张", "李", "王", "赵", "秦", "孙", "刘", "郭", "周", "罗", "胡", "邓", "马", "杨", "陈", "曹", "石", "肖", "吴", "曾", "丘", "崔"};
String[] ming = new String[] { "磊", "凤", "成", "芬", "华", "芳", "艳", "丽", "奇", "红", "才", "涛", "强", "尚峰", "德友", "学文", "淑贞", "圆圆", "伊利", "天祥", "伯通", "黛玉", "元兵", "玉秋", "雪华", "无忌", "三峰"};
String carNums = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String result = "";
for (int i = 0; i < length; i++) {
String payerName = xing[rd.nextInt(xing.length - 1)] + ming[rd.nextInt(ming.length - 1)] + ",";
String orgCode = ",";

String certYear = (rd.nextInt(25) + 70) + "";
String certMonth = formatStr(rd.nextInt(12)+1, 2);
String certDay = formatStr(rd.nextInt(rd.nextInt(28)+1), 2);
String _certNum = "36242719" + certYear + certMonth + certDay + formatStr(rd.nextInt(999)+1, 3);
int[] xishu = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
String[] weishu = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
char[] certNums = _certNum.toCharArray();
int sum = 0;
for (int j = 0; j < certNums.length; j++) {
sum += certNums[j] * xishu[j];
}
_certNum += weishu[(sum % 11)];
String certNum = _certNum + ",";

char ch1 = carNums.charAt( rd.nextInt(carNums.length() - 1));
char ch2 = carNums.charAt( rd.nextInt(carNums.length() - 1));
String _carNum = "" + ch1 + ch2 + formatStr(rd.nextInt(1000), 3);
String Num = "0A" + _carNum + ",";
String engineNum = "E" + formatStr(rd.nextInt(10000000), 7) + "0000006" + _carNum + ",";
String frameNum = "F" + formatStr(rd.nextInt(10000000), 7) + "0006" + _carNum + ",";
String carType = "0" + (rd.nextInt(3)+1);

String passPeocount = ",";
if(carType.equals("03")){
passPeocount = (rd.nextInt(6)+4) + ",";
}else if(carType.equals("02")){
passPeocount = (rd.nextInt(10)+10) + ",";
}else if(carType.equals("01")){
passPeocount = (rd.nextInt(19)+20) + ",";
}

String emptyMass = ",";
String totalMass = ",";
String passMass = ",";

String displacement = ",";
if(carType.equals("03")){
displacement = ((rd.nextInt(14)+11)/10F) + ",";
}else{
displacement = ",";
}

String useNature = "01,";

int year = rd.nextInt(5) + 2006;
int month = rd.nextInt(12)+1;
int day = rd.nextInt(rd.nextInt(28)+1);
String gainDate = year + "-" + formatStr(month, 2) + "-" + formatStr(day, 2) + ",";

String useName = payerName;
String userPhoneTel = ",";
String address = ",";

float _amount = 0;
if(carType.equals("03")){
_amount = 420;
}else if(carType.equals("02")){
_amount = 480;
}else if(carType.equals("01")){
_amount = 600;
}
String amount = (int)_amount + ",";

String lastYearAmount = ",";
if(rd.nextInt(2) == 1){
float[] lastYearAmounts = {1,2,3,2,1,4,2,3,2,1,3,2,2,2,2,1,2,3,2,2,2,2,1,2,1,2,2,2,3,1,3};
if(carType.equals("03")){
lastYearAmount = (int)(420 * lastYearAmounts[rd.nextInt(lastYearAmounts.length - 1)]) + ",";
}else if(carType.equals("02")){
lastYearAmount = (int)(480 * lastYearAmounts[rd.nextInt(lastYearAmounts.length - 1)]) + ",";
}else if(carType.equals("01")){
lastYearAmount = (int)(600 * lastYearAmounts[rd.nextInt(lastYearAmounts.length - 1)]) + ",";
}
}

String derateAmount = ",";

String latefeeAmount = ",";
if(!",".equals(lastYearAmount)){
latefeeAmount = (rd.nextInt(80) + 30) + ",";
}

String insuranceNum = "440120110101"+ formatStr(rd.nextInt(10), 7) +",";
String signDate = "2011-01-01";

carType += ",";

String row = payerName + orgCode + certNum + carNum + engineNum + frameNum
+ carType + passPeocount + emptyMass + totalMass + passMass + displacement
+ useNature + gainDate + useName + userPhoneTel + address
+ amount + lastYearAmount + derateAmount + latefeeAmount + "\r\n";
result += row;
if(i % 8 == 0 && i != 0){
result += "\r\n";
}
}
writeFile(new File("d:\\文本"), result, "UTF-8");
System.out.println("生成完成!");
}

private static String formatStr(int num, int len)
{
String str = num+"";
len = len - str.length();
for (int i = 0; i < len; i++){
str = "0" + str;
}
return str;
}

/**
* 将字符串写入指定文件
* @param file 文件
* @param str 要写入的字符串
* @param encoding 编码
* @return boolean
*/
public static boolean writeFile(File file, String str, String encoding) {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos, encoding);
bw = new BufferedWriter(osw);
bw.write(str);
bw.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(bw != null){ bw.close(); }
if(osw != null){ osw.close(); }
if(fos != null){ fos.close(); }
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值