您的位置:首页 > 其它

随机生成身份证号和年龄

2015-06-14 21:23 302 查看
package main;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Random;

public class demo {

public static void main(String argv[]) throws IOException {

if (argv.length < 1) {

System.err.println("Parameter missing.");

System.err.println("Usage: input:recall-list;output:rank-list");

return;

}

// Initial

String input = argv[1];

// Input

File file = new File(input);

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream(file), "gbk"));

PrintWriter pw = new PrintWriter(new OutputStreamWriter(

new FileOutputStream(file+"result"), "gbk"), true);

String line = null;

while ((line = br.readLine()) != null) {

String[] seg = line.split("\t");

String id = "";

// 随机生成省、自治区、直辖市代码 1-2

String provinces[] = { "50" ,"50", "50"};

String province = provinces[new Random()

.nextInt(provinces.length - 1)];

// 随机生成地级市、盟、自治州代码 3-4

String citys[] = { "01", "02" };

String city = citys[new Random().nextInt(citys.length - 1)];

// 随机生成县、县级市、区代码 5-6

String countys[] = { "01", "02", "03", "04", "05", "06", "07",

"08", "09", "10", "11", "12", "13", "14", "15", "16", "17",

"18", "19", "22", "23", "24", "25", "26", "27", "28", "29",

"30", "31", "32", "33", "34", "35", "36", "37", "38", "41",

"42", "43", "40", };

String county = countys[new Random().nextInt(countys.length - 1)];

// String nannv = seg[8];

Random rand = new Random();

int nanv = rand.nextInt(1);

if (nanv == 1) {

seg[8] = "男";

} else {

seg[8] = "女";

}

// 随机生成出生年月 7-14

SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");

SimpleDateFormat dft2 = new SimpleDateFormat("yyyy");

Date beginDate = new Date();

Calendar date = Calendar.getInstance();

date.setTime(beginDate);

date.set(Calendar.DATE,

date.get(Calendar.DATE) - new Random().nextInt(365 * 100));

String birth = dft.format(date.getTime());

String birth2 = dft2.format(date.getTime());

seg[9] = String.valueOf(2015 - Integer.parseInt(birth2));

// 随机生成顺序号 15-17

String no = new Random().nextInt(999) + "";

// 随机生成校验码 18

String checks[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8",

"9", "X" };

String check = checks[new Random().nextInt(checks.length - 1)];

// 拼接身份证号码

id = province + city + county + birth + no + check;

seg[10] = id;

System.out.println(line);

for (int i = 0; i < seg.length; i++) {

pw.print(seg[i] + "\t");

}

pw.print("\n");

}

br.close();

pw.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: