gamehiboy 2011-12-29 11:54
浏览 305
已采纳

java读写

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-30 01: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 mao

    text1.txt文件内容
    小名=xiaoming
    小名=xiaominghao
    小名=xiaoming hao
    小名=xiao ming
    小名=xiao ming hao
    小毛=xiaomao
    小毛=xiao mao

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(11条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?