华为通讯录导出再导入到其他平台
如题华为通讯录导出,再导入其他平台会出现名称错误的情况,这里我做了个小程序将导出的通讯录进行处理。程序如下:
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DaoTest {
public static void main(String[] args) {
String sourcePath = "C:\\contacts.vcf";
String targetPath = "C:\\contacts_out.vcf";
try (BufferedReader reader = new BufferedReader(new FileReader(sourcePath))) {
BufferedWriter fos = new BufferedWriter(new FileWriter(targetPath));
String line;
while ((line = reader.readLine()) != null) {
String patternString = "(=[0-9A-Fa-f]{2})+";
Pattern pattern = Pattern.compile(patternString);
String replaceLine = null;
if (line.startsWith("N;") || line.startsWith("ADR;") || line.startsWith("FN;") || line.startsWith("ORG;")){
Matcher matcher = pattern.matcher(line);
while (matcher.find()){
replaceLine = matcher.group();
}
}
if (replaceLine != null){
fos.write(line.replace(replaceLine, decodeName(replaceLine)) + "\n");
} else {
fos.write(line + "\n");
}
}
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static String decodeName(String hexEncodedString) throws UnsupportedEncodingException {
String[] split = hexEncodedString.replaceFirst("=", "").split("=");
byte[] bts = new byte[split.length];
for (int i = 0; i < split.length; i++) {
bts[i] = toByte(split[i]);
}
return new String(bts, "UTF-8");
}
public static byte toByte(String charObj){
return (byte)Integer.parseInt(charObj, 16);
}
}
Java程序直接执行就行,不懂的同学可以留言,后续我会做成在线转换工具。
License:
CC BY 4.0