> 文档中心 > java通讯录 批量生成 .vcf文件, .csv文件批量生成 .vcf,手机自动导入

java通讯录 批量生成 .vcf文件, .csv文件批量生成 .vcf,手机自动导入


java 本excel 通讯录 文件批量生成 .vcf文件, .csv文件批量生成 .vcf

本人原博客:https://www.iteye.com/blog/user/mr-lili-1986-163-com  更多文章,后期将转过来

package test2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.text.translate.UnicodeEscaper;
import org.apache.commons.lang3.text.translate.UnicodeUnescaper;
import org.apache.http.client.utils.URLEncodedUtils;

/**

*// 个人原 iteye博客:http://mr-lili-1986-163-com.iteye.com/
* excel 文件 (必须是 , 隔开的 记事本文件) 批量生成 .vcf文件
* @author Administrator
*
*/
public class Test {

public static void main(String[] args) throws UnsupportedEncodingException {
//E99988E5BF97E6988E

String filePath = "D:\\cone\\WLMContacts.csv";
// "res/";
List list = readTxtFile(filePath);
System.out.println("-------------------");
if(null != list && list.size() > 0){
for (String str : list) {
String[] ts = str.split(",");
String name = ts[0];
String tel = ts[2];
String org = ts[1];
printConnectstr(name, tel, org);
}
}

// String s = "陈志";
// String name = "陈小明2";
// String tel = "13564576688";
// String org = "市场部";
//
// printConnectstr(name, tel, org);

// System.out.println(s+" --的unicode编码是:"+gbEncoding(s));
// System.out.println(gbEncoding(s) + " --转换成中文是:"+decodeUnicode(gbEncoding(s)));

}

/**
* 打印格式
* @param name
* @param tel
* @param org
*/
private static void printConnectstr(String name, String tel, String org) {
System.out.println("BEGIN:VCARD");
System.out.println("VERSION:2.1");
String n1 = getURLEncoderString(name);
System.out.println("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;"+n1+";;;");
System.out.println("FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"+n1);
System.out.println("TEL;CELL:"+tel);
System.out.println("ORG:");
String org1 = getURLEncoderString(org);
System.out.println("TITLE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"+org1);
System.out.println("END:VCARD");
}

/**
* 读cvs“ ,隔开的文件 ”文件
* @param filePath
* @return
*/
public static List readTxtFile(String filePath){
List list = new ArrayList();
try {

String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;

while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);

list.add(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return list;

}

/**
* 转成手机码
* @param str
* @return
*/
public static String getURLEncoderString(String str) {
String result = "";
if (null == str) {
return "";
}
try {
result = java.net.URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String c2 = result.replaceAll("%", "=");//转换成手机通讯艮编码格式
return c2;
}

/**
* 解码
* @param str
* @return
*/
public static String URLDecoderString(String str) {
String result = "";
if (null == str) {
return "";
}
try {
result = java.net.URLDecoder.decode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}

// /*
// * 中文转unicode编码
// */
// public static String gbEncoding(final String gbString) {
// char[] utfBytes = gbString.toCharArray();
// String unicodeBytes = "";
// for (int i = 0; i < utfBytes.length; i++) {
// String hexB = Integer.toHexString(utfBytes[i]);
// if (hexB.length() <= 2) {
// hexB = "00" + hexB;
// }
// unicodeBytes = unicodeBytes + "\\u" + hexB;
// }
// return unicodeBytes;
// }
// /*
// * unicode编码转中文
// */
// public static String decodeUnicode(final String dataStr) {
// int start = 0;
// int end = 0;
// final StringBuffer buffer = new StringBuffer();
// while (start > -1) {
// end = dataStr.indexOf("\\u", start + 2);
// String charStr = "";
// if (end == -1) {
// charStr = dataStr.substring(start + 2, dataStr.length());
// } else {
// charStr = dataStr.substring(start + 2, end);
// }
// char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
// buffer.append(new Character(letter).toString());
// start = end;
// }
// return buffer.toString();
// }

}

------------------------------------------------------

测试文件内容格式:记事本,以,隔开就行

张小格,办公会领导,13412422268
刘小明,事业部,18587456548
张磊,事业部,15954270846

ORG:代表的就是部门

生成格式,与导入  手机 的vcf 一致,生成后,可直接导入手机,   下面是两个事例生成的字符串结构

BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;=E4=BB=98=E5=8D=8E=E8=8C=82;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=BB=98=E5=8D=8E=E8=8C=82
TEL;CELL:13250345128
ORG:
TITLE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E4=B8=AD=E5=BF=83=E9=A2=86=E5=AF=BC
END:VCARD
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;=E5=88=98=E5=AE=8F;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=88=98=E5=AE=8F
TEL;CELL:13250345128
ORG:
TITLE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=92=A8=E8=AF=A2=E4=BA=8B=E4=B8=9A=E9=83=A8
END:VCARD