姓名转拼音

项目中遇到为用户分配用户名,规则是若姓名为两个字,则用全拼表示,若姓名为三个字,则用姓全拼,名的首字母表示;如“张三”对应用户名就是“zhangsan”,“李小四”对应用户名就是“lixs”,本来打算用Excel的宏命令实现,可是对特殊字,生僻字的转化不太理想;于是引用了java的pinyin4j来实现。
考虑到用户名的唯一性,在用户名相同时依次在用户名后加数字,如用户名为“zhangsan”的出现了不止一次,则分别用"zhangsan","zhangsan1","zhangsan2"......
引入pinyin4j-1.1.0.jar
代码如下:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

/**
 * 汉字转拼音工具类 基于 pinyin4j
 */
public class PinyinUtil {

	private static PinyinUtil instance;

	public static PinyinUtil getInstance() {
		if (instance == null) {
			instance = new PinyinUtil();
		}
		return instance;
	}

	private HanyuPinyinOutputFormat outputFormat = null;

	private HanyuPinyinOutputFormat getOutputFormat() {
		if (outputFormat == null) {
			outputFormat = new HanyuPinyinOutputFormat();
			outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
			outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		}
		return outputFormat;
	}

	//获取字符串中所有字符首字母
	public String getStringInitial(String str) {
		StringBuffer sbf = new StringBuffer();
		if (str != null) {
			for (int i = 0; i < str.length(); i++) {
				sbf.append(getCharInitial(str.charAt(i)));
			}
		}
		return sbf.toString();
	}

	 //获取中文字符首字母
	public String getCharInitial(char c) {
		try {
			String[] strs = PinyinHelper.toHanyuPinyinStringArray(c,
					getOutputFormat());
			String initial = "";
			if (strs != null && strs.length > 0) {
				String str = strs[0];
				if (str != null && str.length() > 0) {
					initial += str.charAt(0);
				}
			}
			return initial;
		} catch (Exception ex) {
			ex.printStackTrace();
			return c + "";
		}
	}
	
	 //获取中文字符串全拼
	public String getStringPinyin(String chines) {
		char[] nameChar = chines.toCharArray();
		String pinyinStr = "";
		for (int i = 0; i < nameChar.length; i++) {
			try {
				char cha = nameChar[i];
				if (nameChar[i] > 128) {
					pinyinStr += PinyinHelper.toHanyuPinyinStringArray(cha,
							getOutputFormat())[0];
				}
			} catch (Exception ex) {
				ex.printStackTrace();
				pinyinStr += nameChar[i];
			}
		}
		return pinyinStr;
	}

	public static void main(String[] args) {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			String str = "";
			fis = new FileInputStream("resource/username.txt");// FileInputStream


			// 从文件系统中的某个文件中获取字节
			isr = new InputStreamReader(fis);// InputStreamReader 是字节流通向字符流的桥梁,
			br = new BufferedReader(isr);// 从字符输入流中读取文件中的内容
			int index = 0;
			List nameList = new ArrayList();


			while ((str = br.readLine()) != null) {
				String tmp = "";
				if (str.length() == 2) {
					tmp = PinyinUtil.getInstance().getStringPinyin(str);
				} else if (str.length() == 3) {
					tmp = PinyinUtil.getInstance().getStringPinyin(
							str.substring(0, 1));
					tmp += PinyinUtil.getInstance().getStringInitial(
							str.substring(1, 3));
				}
				index++;
				if (nameList.contains(tmp)
						&& Collections.frequency(nameList, tmp) >= 1) {
					System.out.println(tmp
							+ Collections.frequency(nameList, tmp));
					nameList.add(tmp);
				} else {
					nameList.add(tmp);
					System.out.println(tmp);
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("找不到指定文件");
		} catch (IOException e) {
			System.out.println("读取文件失败");
		} finally {
			try {
				br.close();
				isr.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

输入:姓名转拼音_第1张图片,输出:姓名转拼音_第2张图片


你可能感兴趣的:(Java)