package com.cmcc.tools;
import org.apache.commons.lang.StringUtils;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Random;
/**
* 构造手机号码和18位code值并写入文件
*/
public class createPhoneAndCode {
//中国移动
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"
};
public static void main(String[] args) throws IOException {
//设置生成文件的路径
String filePath = "C:\\Users\\EDZ\\Desktop\\data.csv";
//输入需要生成的数据条数
int number = 3;
System.out.println("需要生成数据 : " + number + "条");
long writeStringToCsvTime = System.currentTimeMillis();
String dataStr = getCsvDataStr(number);
writeStringToCsv(dataStr, filePath, true);
System.out.println("已完成数据生成,耗时 : " + (System.currentTimeMillis() - writeStringToCsvTime) + " ms");
}
/**
* 获取写入CSV的数据
* @return String 写入CSV的内容拼装成一个字符串的
* csv文件默认以,分割。以换行符来换行,phone和code中添加,以换列,拼接格式为phone为一列数据,code为一列数据
*/
private static String getCsvDataStr(int num) throws IOException{
String phone=null;
String code=null;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < num; i++) {
phone=createMobile();
code= getCode(18);
sb.append(phone).append(",").append(code).append('\r').append('\n');
}
return sb.toString();
}
/**
* 生成手机号
*/
public static String createMobile() {
StringBuilder sb = new StringBuilder();
Random random = new Random();
int op = random.nextInt(3);//随机运营商标志位
String mobileThree;//手机号前三位
int temp;
switch (op) {
case 0:
mobileThree = CHINA_MOBILE[random.nextInt(CHINA_MOBILE.length)];
break;
case 1:
mobileThree = CHINA_UNICOM[random.nextInt(CHINA_UNICOM.length)];
break;
case 2:
mobileThree = CHINA_TELECOME[random.nextInt(CHINA_TELECOME.length)];
break;
default:
mobileThree = "op标志位有误!";
break;
}
if (mobileThree.length() > 3) {
return mobileThree;
}
sb.append(mobileThree);
//生成手机号后8位
for (int i = 0; i < 8; i++) {
temp = random.nextInt(10);
sb.append(temp);
}
return sb.toString();
}
/**
* 生成code
* @param length code长度
*/
private static String getCode( int length) throws IOException {
String code = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
String str = random.nextInt(2) % 2 == 0 ? "num" : "char";
if ("char".equalsIgnoreCase(str)) { // 产生字母
int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
code += (char) (nextInt + random.nextInt(26));
} else if ("num".equalsIgnoreCase(str)) { // 产生数字
code += String.valueOf(random.nextInt(10));
}
}
return code;
}
/**
* 一次性向CSV写入内容
* @param dataStr 数据字符串
* @param csvFilePath 文件路径
* @param append 是否向已存在的CSV追加内容
*/
public static void writeStringToCsv(String dataStr, String csvFilePath, boolean append) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(csvFilePath, append);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter)) {
// 将数据写入到缓存中
if (StringUtils.isNotBlank(dataStr)) {
bufferedWriter.write(dataStr);
}
//把缓存中的数据输出到CSV文件
bufferedWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行结果: