//今天组长让我写一个查重的的程序来给一个词库查重 把相同的删除
//一开始我绞尽脑汁想用嵌套的for循环怎么都有bug 原因是&&和&的理解错误
//后来我想到了用Map集合 应为它的键值不可能相同 这样就轻松解决了重复的问题 恩恩哈哈········
//Map <String,String> pp=new HashMap<String,String>; 他就是一个很好的查重的机器啊
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Shangchu {
/**
* @param args
*/
BufferedReader br = null;
StringBuffer sb1 = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
public PrintWriter pw=null;
public Map<String,String> ciku=new HashMap<String,String>();
public List<String> hanzi=new ArrayList<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Shangchu sc=new Shangchu();
sc.chaChong("pinyinciku.txt");
}
public void chaChong(String txt) {
int s=0;
try {
br= new BufferedReader(new FileReader(txt));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件读取失败");
}
try {
while((s = br.read()) != -1)
{
if(s != '\n')
if(!((s < 'a'||s>'z')&& (s != ' ')&&(s != '\'')))
{
sb1.append((char)s); //添加拼音
}
else
{
sb2.append((char)s); //添加汉字
}
else
{
ciku.put(sb2.toString(), sb1.toString());
sb1.delete(0,sb1.capacity()-1);
sb2.delete(0,sb2.capacity()-1);
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("字符串添加失败");
}
finally
{
if (br!=null)
{
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("br关闭失败");
}
}
}
try {
pw=new PrintWriter(new FileWriter(new File ("a.txt")));
Set<Map.Entry<String,String>> entryset=ciku.entrySet();
Iterator<Map.Entry<String,String>> its =entryset.iterator();
while (its.hasNext())
{
Map.Entry<String,String> mm=its.next();
String hanzi= mm.getKey();
String pinyin=mm.getValue();
pw.append(pinyin);
pw.append(hanzi);
pw.append('\n');
System.out.println(pinyin+" ````` "+hanzi);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (pw!=null)
{
pw.close();
}
}
}
}