- 注册时间
- 2013-4-19
- 最后登录
- 2024-9-26
- 阅读权限
- 200
- 积分
- 6404
- 精华
- 0
- 帖子
- 640
|
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import java.util.Random;
public class VCard {
public static void main(String[] args) {
VCard vCard = new VCard();
for (int i = 0; i < 2500; i++) {
String name = string2QuotedPrintable(randomName());
String tel1 = string2QuotedPrintable(randomNum());
String tel2 = string2QuotedPrintable(randomNum());
String tel3 = string2QuotedPrintable(randomNum());
String tel4 = string2QuotedPrintable(randomNum());
String tel5 = string2QuotedPrintable(randomNum());
vCard.write2File(name, tel1, tel2, tel3, tel4, tel5, "abc@163.com", "北京公司");
}
}
private static String[] a1 = { "张", "刘", "孙", "赵", "李", "王", "吴", "周", "公孙", "诸葛", "关", "马" };
private static String[] a2 = { "国庆", "浩", "笑", "大大", "小小", "圆圆", "芳芳", "超" };
public static String randomName() {
Random random = new Random();
int f = random.nextInt(a1.length);
int f1 = random.nextInt(99);
int g = random.nextInt(a2.length);
int g1 = random.nextInt(99);
return a1[f] + "_" + a2[g] + g1;
}
/***
* BitSet of printable charwrite2Fileacters as defined in RFC 1521.
*/
private static final BitSet PRINTABLE_CHARS = new BitSet(256);
private static byte ESCAPE_CHAR = '=';
private static byte TAB = 9;
private static byte SPACE = 32;
// Static initializer for printable chars collection
static {
// alpha characters
for (int i = 33; i <= 60; i++) {
PRINTABLE_CHARS.set(i);
}
for (int i = 62; i <= 126; i++) {
PRINTABLE_CHARS.set(i);
}
PRINTABLE_CHARS.set(TAB);
PRINTABLE_CHARS.set(SPACE);
}
// 字符串转Quoted-Printable
private static String string2QuotedPrintable(String str) {
try {
byte[] bytes = str.getBytes("utf-8");
byte[] result = encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
System.out.println(new String(result));
return new String(result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public static byte[] encodeQuotedPrintable(BitSet printable, byte[] bytes) {
if (bytes == null) {
return null;
}
if (printable == null) {
printable = PRINTABLE_CHARS;
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0; i < bytes.length; i++) {
int b = bytes[i];
if (b < 0) {
b = 256 + b;
}
if (printable.get(b)) {
buffer.write(b);
} else {
encodeQuotedPrintable(b, buffer);
}
}
return buffer.toByteArray();
}
public static String randomNum() {
Random random = new Random();
int num = random.nextInt(1000000000 - 2);
int f = random.nextInt(9);
String str = String.format("1%d%09d", f, num);
return str;
}
private static void encodeQuotedPrintable(int b, ByteArrayOutputStream buffer) {
buffer.write(ESCAPE_CHAR);
char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16));
char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, 16));
buffer.write(hex1);
buffer.write(hex2);
}
// 保存到vcf文件
private OutputStream write2File(String name, String tel1, String tel2, String tel3, String tel4, String tel5, String mail, String org) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("karl.vcf", true));
bw.write("BEGIN:VCARD");
bw.newLine();
bw.write("VERSION:2.1");
bw.newLine();
if (name != null) {
bw.write("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;" + name + ";;;");// 机器
bw.newLine();
bw.write("FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:" + name);
bw.newLine();
}
bw.write("TEL;CELL:150 0000 8888");
bw.newLine();
bw.write("TEL;CELL:12 088 880 000");
bw.newLine();
bw.write("TEL;HOME:154 7893 25");
bw.newLine();
bw.write("TEL;WORK:788 9164 61");
bw.newLine();
bw.write("EMAIL;HOME:gfjd@gmail.com");
bw.newLine();
bw.write("ORG;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=8C=97=E4=BA=AC=E5=85=AC=E5=8F=B8");// 北京公司
bw.newLine();
bw.write("END:VCARD");
bw.newLine();
bw.flush();
bw.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
|
|