java通讯录 批量生成 .vcf文件, .csv文件批量生成 .vcf,手机自动导入
java 本excel 通訊錄 文件批量生成 .vcf文件, .csv文件批量生成 .vcf
本人原博客:https://www.iteye.com/blog/user/mr-lili-1986-163-com? 更多文章,后期將轉(zhuǎn)過來
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<String> 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) + " --轉(zhuǎn)換成中文是:"+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<String> readTxtFile(String filePath){
List list = new ArrayList<String>();
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("讀取文件內(nèi)容出錯");
e.printStackTrace();
}
return list;
}
/**
* 轉(zhuǎn)成手機碼
* @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("%", "=");//轉(zhuǎn)換成手機通訊艮編碼格式
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;
}
// /*
// * 中文轉(zhuǎn)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編碼轉(zhuǎn)中文
// */
// 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();
// }
}
------------------------------------------------------
測試文件內(nèi)容格式:記事本,以,隔開就行
張小格,辦公會領(lǐng)導(dǎo),13412422268
劉小明,事業(yè)部,18587456548
張磊,事業(yè)部,15954270846
ORG:代表的就是部門
生成格式,與導(dǎo)入? 手機 的vcf 一致,生成后,可直接導(dǎo)入手機,? ?下面是兩個事例生成的字符串結(jié)構(gòu)
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
總結(jié)
以上是生活随笔為你收集整理的java通讯录 批量生成 .vcf文件, .csv文件批量生成 .vcf,手机自动导入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 加班ing
- 下一篇: 使用c语言计算3阶行列式