import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.Calendar; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class IdCardGenerator { public static final Map<String, Integer> areaCode = new HashMap<String, Integer>(); static {//可根据自己的需求放入需要的行政编码 IdCardGenerator.areaCode.put("钦南区", 450702); IdCardGenerator.areaCode.put("钦北区", 450703); IdCardGenerator.areaCode.put("灵山县", 450721); IdCardGenerator.areaCode.put("浦北县", 450722); IdCardGenerator.areaCode.put("贵港市", 450800); IdCardGenerator.areaCode.put("市辖区", 450801); IdCardGenerator.areaCode.put("港北区", 450802); IdCardGenerator.areaCode.put("港南区", 450803); IdCardGenerator.areaCode.put("覃塘区", 450804); IdCardGenerator.areaCode.put("平南县", 450821); IdCardGenerator.areaCode.put("桂平市", 450881); IdCardGenerator.areaCode.put("玉林市", 450900); IdCardGenerator.areaCode.put("市辖区", 450901); IdCardGenerator.areaCode.put("玉州区", 450902); IdCardGenerator.areaCode.put("容 县", 450921); IdCardGenerator.areaCode.put("陆川县", 450922); IdCardGenerator.areaCode.put("博白县", 450923); IdCardGenerator.areaCode.put("兴业县", 450924); IdCardGenerator.areaCode.put("北流市", 450981); IdCardGenerator.areaCode.put("百色市", 451000); IdCardGenerator.areaCode.put("市辖区", 451001); IdCardGenerator.areaCode.put("右江区", 451002); IdCardGenerator.areaCode.put("田阳县", 451021); IdCardGenerator.areaCode.put("田东县", 451022); IdCardGenerator.areaCode.put("平果县", 451023); IdCardGenerator.areaCode.put("德保县", 451024); IdCardGenerator.areaCode.put("靖西县", 451025); IdCardGenerator.areaCode.put("那坡县", 451026); IdCardGenerator.areaCode.put("凌云县", 451027); IdCardGenerator.areaCode.put("乐业县", 451028); } /** * 生成方法 * * @return */ public String generate() { StringBuilder generater = new StringBuilder(); generater.append(this.randomAreaCode()); generater.append(this.randomBirthday()); generater.append(this.randomCode()); generater.append(this.calcTrailingNumber(generater.toString().toCharArray())); return generater.toString(); } /** * 随机地区 * * @return */ public int randomAreaCode() { int index = (int) (Math.random() * IdCardGenerator.areaCode.size()); Collection<Integer> values = IdCardGenerator.areaCode.values(); Iterator<Integer> it = values.iterator(); int i = 0; int code = 0; while (i < index && it.hasNext()) { i++; code = it.next(); } return code; } public static String maleFemale(String idNumber) { String Flag = ""; if (idNumber.length() >= 17) { String strflag = idNumber.substring(16, 17); //System.out.println(strflag); if (Integer.valueOf(strflag) % 2 == 0) { Flag = "F"; } else Flag = "M"; } return Flag; } /** * 随机出生日期 * * @return */ public String randomBirthday() { Calendar birthday = Calendar.getInstance(); birthday.set(Calendar.YEAR, (int) (Math.random() * 60) + 1950); birthday.set(Calendar.MONTH, (int) (Math.random() * 12)); birthday.set(Calendar.DATE, (int) (Math.random() * 31)); StringBuilder builder = new StringBuilder(); builder.append(birthday.get(Calendar.YEAR)); long month = birthday.get(Calendar.MONTH) + 1; if (month < 10) { builder.append("0"); } builder.append(month); long date = birthday.get(Calendar.DATE); if (date < 10) { builder.append("0"); } builder.append(date); return builder.toString(); } /* * <p>18位身份证验证</p> * 根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。 * 排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。 * 第十八位数字(校验码)的计算方法为: * 1.将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 * 2.将这17位数字和系数相乘的结果相加。 * 3.用加出来和除以11,看余数是多少? * 4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为1 0 X 9 8 7 6 5 4 3 2。 * 5.通过上面得知如果余数是2,就会在身份证的第18位数字上出现罗马数字的Ⅹ。如果余数是10,身份证的最后一位号码就是2。 */ public char calcTrailingNumber(char[] chars) { if (chars.length < 17) { return ‘ ‘; } int[] c = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; char[] r = {‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘}; int[] n = new int[17]; int result = 0; for (int i = 0; i < n.length; i++) { n[i] = Integer.parseInt(chars[i] + ""); } for (int i = 0; i < n.length; i++) { result += c[i] * n[i]; } return r[result % 11]; } /** * 随机产生3位数 * * @return */ public String randomCode() { int code = (int) (Math.random() * 1000); if (code < 10) { return "00" + code; } else if (code < 100) { return "0" + code; } else { return "" + code; } } /** * 将数据写入txt文件 * * @param filePath txt文件地址 * @param content 需要插入的内容 */ public static void contentToTxt(String filePath, String content) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath), true)); writer.write("\n" + content); writer.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String template = "|#name|#logo|0|李四|#FM|#birthday|LI SI|1|#IDNumber|#mobile|36200|0000||||||||||||$$"; IdCardGenerator g = new IdCardGenerator(); String name = "tuhuka"; String logo = "362"; for (int i = 0; i < 10; i++) {//需要几个,就用几个for循环 String idNumber = g.generate(); String maleFemale = maleFemale(idNumber); String birthday = idNumber.substring(6, 14); String mobile = Mobile.getMobile(1); System.out.println(template.replace("#name", name).replace("#logo", logo).replace("#FM", maleFemale).replace("#birthday",birthday).replace("#IDNumber",idNumber).replace("#mobile",mobile)); /*System.out.println(idNumber); System.out.println(maleFemale); System.out.println(birthday); System.out.println(mobile);*/ //contentToTxt("E:\\IdCardGenerator.txt",g.generate()); //将生成的身份证存入F下的IdCardGenerator.txt里面 } } }
mobile.ava
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; import java.util.Scanner; public class Mobile { //中国移动 public static final String[] CHINA_MOBILE = { "134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159", "182", "183", "184", "187", "188", "178", "147", "172", "198" }; //中国联通 public static final String[] CHINA_UNICOM = { "130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186", "166" }; //中国电信 public static final String[] CHINA_TELECOME = { "133", "149", "153", "173", "177", "180", "181", "189", "199" }; /** * 生成手机号 * * @param op 0 移动 1 联通 2 电信 */ public static String createMobile(int op) { StringBuilder sb = new StringBuilder(); Random random = new Random(); String mobile01;//手机号前三位 int temp; switch (op) { case 0: mobile01 = CHINA_MOBILE[random.nextInt(CHINA_MOBILE.length)]; break; case 1: mobile01 = CHINA_UNICOM[random.nextInt(CHINA_UNICOM.length)]; break; case 2: mobile01 = CHINA_TELECOME[random.nextInt(CHINA_TELECOME.length)]; break; default: mobile01 = "op标志位有误!"; break; } if (mobile01.length() > 3) { return mobile01; } sb.append(mobile01); //生成手机号后8位 for (int i = 0; i < 8; i++) { temp = random.nextInt(10); sb.append(temp); } return sb.toString(); } public static String getMobile(int number) { Random random = new Random(); StringBuilder sb = new StringBuilder(); int op = random.nextInt(3);//随机运营商标志位 sb.append(createMobile(op)); return sb.toString(); } /* public static void main(String[] args) throws IOException { Scanner scan = new Scanner(System.in); System.out.println("请输入生成的手机号个数:"); int number = scan.nextInt(); Random random = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 1; i <= number; i++) { int op = random.nextInt(3);//随机运营商标志位 sb.append(createMobile(op)); if (i % 10 == 0) { sb.append("\n"); } else { sb.append("\t"); } } //写入文件 FileOutputStream fos = new FileOutputStream(new File("F:/mobile.txt")); fos.write(sb.toString().getBytes()); fos.close(); System.out.println(number + "个号码生成成功!"); }*/ }
输出结果:
|途虎卡|362|0|李四|F|20010327|LI SI|1|450923200103278368|18098540405|36200|0000||||||||||||$$
|途虎卡|362|0|李四|F|19540826|LI SI|1|451027195408266900|15556926538|36200|0000||||||||||||$$
|途虎卡|362|0|李四|F|19800909|LI SI|1|451021198009097228|13380164336|36200|0000||||||||||||$$
|途虎卡|362|0|李四|F|20090909|LI SI|1|450821200909091609|17225760579|36200|0000||||||||||||$$
|途虎卡|362|0|李四|M|19631015|LI SI|1|450821196310155072|13094181610|36200|0000||||||||||||$$
|途虎卡|362|0|李四|F|19870422|LI SI|1|451002198704223266|17612707803|36200|0000||||||||||||$$
|途虎卡|362|0|李四|F|19791026|LI SI|1|450922197910265925|17344159113|36200|0000||||||||||||$$
|途虎卡|362|0|李四|F|19690819|LI SI|1|450722196908199644|13875769250|36200|0000||||||||||||$$
|途虎卡|362|0|李四|M|19590214|LI SI|1|451022195902145897|15356471057|36200|0000||||||||||||$$
|途虎卡|362|0|李四|M|20070304|LI SI|1|45092320070304803X|18967626122|36200|0000||||||||||||$$
原文:https://www.cnblogs.com/weny/p/12324102.html