-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVCFParser.java
More file actions
177 lines (160 loc) · 6.15 KB
/
VCFParser.java
File metadata and controls
177 lines (160 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.Collator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
// java -cp "$env:USERPROFILE\.m2\repository\com\googlecode\ez-vcard\ez-vcard\0.12.0\ez-vcard-0.12.0.jar" .\VCFParser.java
/**
* <dependency>
* <groupId>com.googlecode.ez-vcard</groupId>
* <artifactId>ez-vcard</artifactId>
* <version>0.12.0</version>
* </dependency>
*/
import ezvcard.Ezvcard;
import ezvcard.VCard;
import ezvcard.property.FormattedName;
public class VCFParser {
// 排序a-z
public static void sort(List<VCard> vCards) {
System.out.println(vCards.size() + "个联系人待排序");
Collator collator = Collator.getInstance(Locale.CHINESE);
vCards.sort((a, b) -> {
FormattedName fn1 = a.getFormattedName();
FormattedName fn2 = b.getFormattedName();
if (fn1 == null || fn2 == null) {
return 0;
}
return collator.compare(fn1.getValue(), fn2.getValue());
});
System.out.println("排序完成");
}
// 扫描指定路径下的vcf文件
public static List<Path> scan(Path path) throws IOException {
System.out.println("正在扫描" + path.toString() + "下的文件...");
List<Path> vcfFiles = Files.list(path)
.filter(p -> p.toString().toLowerCase().endsWith(".vcf"))
.collect(Collectors.toList());
System.out.println("扫描完成,共扫描到" + vcfFiles.size() + "个文件");
return vcfFiles;
}
// 读取vcf
public static List<VCard> read(String pathStr) throws IOException {
System.out.println("读取" + pathStr + "...");
Path path = Paths.get(pathStr);
List<VCard> mergeList = null;
if (path.toFile().isFile()) {
// 文件
mergeList = Ezvcard.parse(path).all();
} else if (path.toFile().isDirectory()) {
// 目录
List<Path> vcfFiles = scan(path);
if (vcfFiles.size() > 0) {
mergeList = new ArrayList<>();
}
for (Path vcfFile : vcfFiles) {
System.out.println("读取" + vcfFile + "...");
List<VCard> vCard = Ezvcard.parse(vcfFile).all();
VCard card = vCard.get(0);
mergeList.add(card);
}
}
if (mergeList == null) {
System.out.println("读取失败");
System.exit(1);
}
System.out.println("读取完成");
System.out.println("共读取" + mergeList.size() + "个联系人");
return mergeList;
}
// 创建一个输出流
public static OutputStreamWriter getOneOutputStreamWriter(String pathStr, boolean append) throws IOException {
if (pathStr == null) {
pathStr = "./" + System.currentTimeMillis() + ".vcf";
System.out.println("创建文件" + pathStr);
}
Path path = Paths.get(pathStr);
if (!path.toFile().exists()) {
Files.createDirectories(path.getParent());
path.toFile().createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(path.toFile(), append);
return new OutputStreamWriter(fileOutputStream, "utf-8");
}
// 按照FN写出到不同的文件
public static void map(List<VCard> vCards) throws IOException {
System.out.println("mapping...");
for (VCard vCard : vCards) {
String fn = vCard.getFormattedName().getValue();
try (OutputStreamWriter osw = getOneOutputStreamWriter("./tmp/" + fn + ".vcf", true)) {
Ezvcard.write(vCard).prodId(false).go(osw);
}
}
System.out.println("mapped");
}
// 合并不冲突的文件
public static List<VCard> merge() throws IOException {
List<Path> vcfFiles = scan(Paths.get("./tmp"));
// 合并所有的vcf文件
List<VCard> mergeList = new ArrayList<>();
for (Path vcfFile : vcfFiles) {
List<VCard> vCard = Ezvcard.parse(vcfFile).all();
if (vCard.size() != 1) {
// 如果vcf文件包含不止一个vcard,跳过
continue;
}
System.out.println("正在合并" + vcfFile);
VCard card = vCard.get(0);
mergeList.add(card);
// 删除文件
vcfFile.toFile().delete();
}
System.out.println("合并完成,共合并" + mergeList.size() + "个联系人");
return mergeList;
}
// 写出vcf
public static void write(List<VCard> vCards) throws IOException {
System.out.println("写出" + vCards.size() + "个联系人");
try (OutputStreamWriter osw = getOneOutputStreamWriter(null, false)) {
Ezvcard.write(vCards).prodId(false).go(osw);
System.out.println("写出完成");
}
}
// 处理冲突
public static void handleConflict() throws IOException {
List<Path> paths = scan(Paths.get("./tmp"));
if (paths.size() < 1) {
System.out.println("恭喜,没有冲突需要处理");
return;
}
for (int i = 0; i < paths.size(); i++) {
Path path = paths.get(i);
System.out.print((i + 1) + "/" + paths.size() + " " + path);
Runtime.getRuntime().exec("cmd /c code " + path.toString());
// 按任意键继续
System.in.read();
System.in.read();// 回车会被当成两个字符
}
List<VCard> vCards = read("./tmp");
sort(vCards);
write(vCards);
}
// 处理vcf文件
public static void process(String pathStr) throws IOException {
List<VCard> vCards = read(pathStr);
map(vCards);
List<VCard> mergedCards = merge();
sort(mergedCards);
write(mergedCards);
handleConflict();
}
public static void main(String[] args) throws IOException {
process("./1691723970765.vcf");
}
}