[code="java"]
public static void main(String[] args) throws Exception {
adjacentLetters();
}
public static void adjacentLetters() throws Exception {
String inFile = "D:\\data.txt";
String outFile = inFile;
BufferedReader bs = null;
BufferedWriter bw = null;
try{
bs = new BufferedReader(new FileReader(new File(inFile)));
char[] ch = new char[3];
char c = 0;
char oc = 0;
Map<String, Integer> outData = new TreeMap<String, Integer>();
String key = "";
Integer sum = 0;
while (bs.read(ch) != -1) {
c = ch[0];
if(c == '*' || c == '#'){
oc = 0;
continue;
}
if(oc == c || (oc + 1) == c || (oc - 1) == c){
key = String.valueOf(oc) + String.valueOf(c);
sum = outData.get(key);
if(sum == null){
sum = 0;
}
outData.put(key, ++sum);
}
oc = c;
}
//bs.close();
//bs = null;
if(!outData.isEmpty()){
//true 追加 false不追加(至文件尾)
bw = new BufferedWriter(new FileWriter(new File(outFile), false));
for(Entry<String, Integer> entry: outData.entrySet()){
bw.write(entry.getKey() + ":" + entry.getValue() + "\r\n");
}
}
}finally{
if(bs != null)
bs.close();
if(bw != null)
bw.close();
}
}
[/code]