我们可能存在这样的一个文件:
apple
define
window
run
…
现在想对其分类, 并以Markdown无序列表的形式输出, 如: 常见单词缩写.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public final class AbbreviationConverter {
public static void main(String[] args) throws Exception {
try {
InputStream in = new FileInputStream(new File("src/abbreviation-src.txt"));
OutputStream out = new FileOutputStream(new File("src/abbreviation-dest.txt"));
convert(in, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private static void convert(InputStream in, OutputStream out) throws Exception, Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
String line = null;
List<String> abbrList = new ArrayList<String>();
Map<String, List<String>> abbrMap = new HashMap<>();
try {
while ((line = br.readLine()) != null) {
if (line.length() > 0) {
abbrList.add(line);
}
}
// sort by ascii
// if don't sort, the src.txt's order will change the final result
abbrList.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);
}
});
String currLine = null;
String nextLine = null;
char currFirstChar = ' ';
char nextFirstChar = ' ';
int i = -1;
int listLen = abbrList.size();
List<String> tmpList = new ArrayList<>();
while (++i < listLen) {
currLine = abbrList.get(i);
currFirstChar = currLine.charAt(0);
try {
nextLine = abbrList.get(i + 1);
nextFirstChar = nextLine.charAt(0);
if (nextFirstChar == currFirstChar) {
tmpList.add(currLine);
} else {
tmpList.add(currLine);
abbrMap.put((char) (currFirstChar - 32) + "", tmpList);
// 引用类型会清空之前的数据,也就是会得到 A -> []
// tmpList.clear();
tmpList = new ArrayList<>();
}
} catch (Exception e) {
// now currLine = abbrList.get(listLen - 1);
//
// if (currFirstChar == abbrList.get(listLen - 2).charAt(0))
// {
// tmpList.add(currLine);
// abbrMap.put((char)(currFirstChar - 32) + "", tmpList);
// } else {
// tmpList = new ArrayList<>();
//
// tmpList.add(currLine);
// abbrMap.put((char)(currFirstChar - 32) + "", tmpList);
// }
if (currFirstChar != abbrList.get(listLen - 2).charAt(0)) {
tmpList = new ArrayList<>();
}
tmpList.add(currLine);
abbrMap.put((char) (currFirstChar - 32) + "", tmpList);
}
}
Set<String> keys = abbrMap.keySet();
for (String key : keys) {
List<String> tmpResultList = abbrMap.get(key);
bw.write("- " + key + "\r\n");
for (String tmpResult : tmpResultList) {
bw.write(" " + tmpResult + "\r\n");
}
bw.write("\r\n");
}
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}