asysbang

 找回密码
 立即注册
查看: 2299|回复: 0
打印 上一主题 下一主题

随机生成联系人数据

[复制链接]

513

主题

2

好友

6404

积分

管理员

Rank: 80Rank: 80Rank: 80Rank: 80Rank: 80

最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2020-8-21 10:30:50 |只看该作者 |倒序浏览
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;
        }

}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

Archiver|手机版|aSys-帮 ( 京ICP备13033689号 )

GMT+8, 2024-10-5 21:23 , Processed in 0.044177 second(s), 19 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部