text.txt文件里有很多数据,每行里有一条数据:第一行:小名,第二行:xiaoming,第三行:xiaomao。。,如何读取text.txt里的数据,将小名转化为拼音,英文不做处理,即小名---->xiaoming,xiao ming,xiao ming hao,xiaominghao,然后和text.txt里的数据比对,如果text里有xiaoming,则将小名=xiaoming,写入text1.txt文件里,求全部代码,
12条回答 默认 最新
- carvin_happy 2011-12-29 17:49关注
[code="java"]
package com.carvin.questions;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;public class Spell {
/**
* 获取汉字串拼音,英文字符不变
*
* @param chinese
* 汉字串
* @return 汉语拼音
*/
public static String cn2Spell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i],
defaultFormat)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString();
}/** * 获取汉字串拼音List,英文字符不变 * * @param chinese * 汉字串 * @return 汉语拼音 */ public static List<String> cn2SpellList(String chinese) { if(chinese == null || chinese.trim().equals("")) { throw new IllegalArgumentException("chinese name is not null and empty"); } String tempChinese = chinese.trim().replaceAll("\\s*", ""); int len = tempChinese.length(); List<String> spellList = new ArrayList<String>(); List<String> combinationList = getCombinationList(len - 1); for(int i=0, getCombinationListLen=combinationList.size(); i<getCombinationListLen; i++) { String combinationStr = combinationList.get(i); StringBuilder strBuilder = new StringBuilder(); for(int j=0; j<len; j++) { strBuilder.append(tempChinese.charAt(j)); if(j != len - 1 && combinationStr.charAt(j) == '1') { strBuilder.append(" "); } } spellList.add(cn2Spell(strBuilder.toString())); } return spellList; } private static List<String> getCombinationList(int spaceCount) { int count = 2 << (spaceCount-1); List<String> combinationList = new ArrayList<String>(); int maxLen = 0; for(int i=0; i<count; i++) { String binaryString = Integer.toBinaryString(i); int len = binaryString.length(); if(len > maxLen) { maxLen = len; } combinationList.add(binaryString); } for(int j=0; j<count; j++) { String combination = combinationList.get(j); int len = combination.length(); if(len < maxLen) { for(int index=len; index<maxLen; index++) { combination = "0" + combination; } combinationList.set(j, combination); } } return combinationList; } /** * 判断字符串是否包含有中文 * @param str 字符串 * @return 包含返回true 不包含返回false */ public static boolean hasCn(String str) { String regEx = "[\\u4e00-\\u9fa5]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.find(); } public static void main(String[] args) throws Exception { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { bufferedReader = new BufferedReader(new FileReader("D:/text.txt")); bufferedWriter = new BufferedWriter(new FileWriter("D:/text1.txt")); List<String> keyList = new ArrayList<String>(); Map<String, List<String>> valueMap = new TreeMap<String, List<String>>(); String line = ""; while ((line = bufferedReader.readLine()) != null) { line = line.trim(); if(hasCn(line)) { valueMap.put(line, cn2SpellList(line)); } else { keyList.add(line); } } for(String name : valueMap.keySet()) { List<String> valueList = valueMap.get(name); for(String spell : valueList) { if(keyList.contains(spell)) { String result = name + "=" + spell + "\n"; bufferedWriter.write(result); } } } } finally { if (bufferedReader != null) { bufferedReader.close(); } if (bufferedWriter != null) { bufferedWriter.close(); } } }
}
[/code]
测试结果:
text.txt文件内容
小名
xiaomao
xiaoqing
xiaoming
xiao ming
xiaominghao
xiao ming hao
xiaoming hao
小毛
xiao maotext1.txt文件内容
小名=xiaoming
小名=xiaominghao
小名=xiaoming hao
小名=xiao ming
小名=xiao ming hao
小毛=xiaomao
小毛=xiao mao本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报