是这样的:
public static HashMap<String, String> telMap = new HashMap<String, String>();
public static HashMap<String, String> loadPhone(String filename)throws IOException {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(
filename), "UTF-8"));
String str = null;
String key = null;
String value =null;
while ((str = br.readLine()) != null) {
String[] ss = str.split(" ", 2);
key = ss[0];
value = ss[1];
telMap.put(key, value);
}
br.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return telMap;
}上面一个方法是用来把一文本1内的内容装到telMap
这文本内的内容是:
11000000 北京市
11010000 市辖区
11010100 东城区
11010200 西城区
11010300 崇文区
类似这样的,前面的是城市区号编码,作为key
后面的是城市名称作为value
再有一个另外的文本2,里面的内容是
晋城
遵义
北京
柳州
海口
大理
德阳想通过文本1给文本2的城市名都配上相应的城市编号
并写到文本3上,文本3的格式和文本1一样
文本2的城市名与文本1的城市名少了个市或县、区这种字,我知道如果是全字匹配,可以用下面的方法解决,但是现在少了个字,怎么解决啊 public void getPhoneAttribution(String cityname) {
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("cityregion.data",true)); if (telMap.containsValue(cityname)) {
bw.write("\n"+cityname+" ");
bw.write(telMap.get(cityname));
} else {
System.out.println("the city's region is null!");
}
bw.flush();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
再用这个containsValue应该不行吧?
public static HashMap<String, String> telMap = new HashMap<String, String>();
public static HashMap<String, String> loadPhone(String filename)throws IOException {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(
filename), "UTF-8"));
String str = null;
String key = null;
String value =null;
while ((str = br.readLine()) != null) {
String[] ss = str.split(" ", 2);
key = ss[0];
value = ss[1];
telMap.put(key, value);
}
br.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return telMap;
}上面一个方法是用来把一文本1内的内容装到telMap
这文本内的内容是:
11000000 北京市
11010000 市辖区
11010100 东城区
11010200 西城区
11010300 崇文区
类似这样的,前面的是城市区号编码,作为key
后面的是城市名称作为value
再有一个另外的文本2,里面的内容是
晋城
遵义
北京
柳州
海口
大理
德阳想通过文本1给文本2的城市名都配上相应的城市编号
并写到文本3上,文本3的格式和文本1一样
文本2的城市名与文本1的城市名少了个市或县、区这种字,我知道如果是全字匹配,可以用下面的方法解决,但是现在少了个字,怎么解决啊 public void getPhoneAttribution(String cityname) {
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("cityregion.data",true)); if (telMap.containsValue(cityname)) {
bw.write("\n"+cityname+" ");
bw.write(telMap.get(cityname));
} else {
System.out.println("the city's region is null!");
}
bw.flush();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
再用这个containsValue应该不行吧?
解决方案 »
- SwingWorker相关问题!!!谢谢各位~
- 怎么设置只能调用显示一个窗口Frame,不能有多个?
- 现在主流的JAVA开发环境是什么工具呀?
- 问高手一个功能怎么实现的,在线等!
- 菜鸟遇到的不解问题(讨论)
- 求高手帮忙写个BAT啊,能够自动检查文件更新。
- 我想用APPLET 读取服务器端文件绘制饼图,读不到数据怎么办?急!快帮帮我吧!
- 请教:BorderLayout的NORTH、CENTER、SOUTH三部分的宽度可不可以自己设定或者给其中一部分指定一个最大宽度?
- java.net.*问题,客户机实例化 socket = new Socket( host, port );为何不成功?
- 这有什么区别吗???
- 怎样使用泛型,根据条件,返回不同的类型?
- java 怎样处理 int 类型溢出?
要么你就从文件里面读取的区名或者城市名称 字符串 再添加一个"市"或者"区"进行匹配
public void getPhoneAttribution(String cityname) {
try{
BufferedWriter bw = new BufferedWriter(new FileWriter("cityregion.data",true));
String cityName1=cityname+"区";
String cityName2=cityname+"市";
if (telMap.containsValue(cityName1)) {
bw.write("\n"+cityName1+" ");
bw.write(telMap.get(cityName1));
}else{
if (telMap.containsValue(cityName2)) {
bw.write("\n"+cityName2+" ");
bw.write(telMap.get(cityName2));
}else {
System.out.println("the city's region is null!");
}
} bw.flush();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
//讀出1.txt中的內容
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\1.txt"), "BIG5"));
Map<String, String> fa = new HashMap<String, String>();
String temp = null;
while ((temp = br.readLine()) != null) {
fa.put(temp.split(" ")[0], temp.split(" ")[1]);
}
br.close(); //讀出2.txt中的內容與fa中的內容進行匹配
BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\2.txt"), "BIG5"));
Map<String, String> fa1 = new HashMap<String, String>();
String temp1 = null;
while ((temp1 = br1.readLine()) != null) {
for (Iterator<Entry<String, String>> it = fa.entrySet().iterator(); it.hasNext();) {
Entry<String, String> e = it.next(); if (e.getValue().matches(".*" + temp1 + ".*")) {
fa1.put(e.getKey(), temp1);
}
}
}
br1.close(); //寫到新文件
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\3.txt"), "BIG5"));
for (Iterator<Entry<String, String>> it = fa1.entrySet().iterator(); it.hasNext();) {
Entry<String, String> e = it.next();
bw.write(e.getKey() + " " + e.getValue() + " \r\n");
}
bw.flush();
bw.close();
这样也关联起来了啊