工具类:自动生成名字工具类

public class CharUtil {
    /**
     * 将字符串转换成相应的字符编码
     * @param s
     * @return
     */
    public static String bytes2HexString(String s) {
        byte[] b = s.getBytes();
        s = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            s = s + hex.toUpperCase();
        }
        return s;
    }

    /**
     * 随机生成汉字
     * @return
     */
    public static String getRandomChar() {
        String str = "";
        int highCode;
        int lowCode;

        Random random = new Random();

        highCode = (176 + Math.abs(random.nextInt(39))); //B0 + 0~39(16~55) 一级汉字所占区
        lowCode = (161 + Math.abs(random.nextInt(93))); //A1 + 0~93 每区有94个汉字

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highCode)).byteValue();
        b[1] = (Integer.valueOf(lowCode)).byteValue();

        try {
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 随机生成2-4个字的名字
     * @return
     */
    public static String getRandomName() {
        Random random = new Random();
        //名字的个数
        int charNum = random.nextInt(4) % 3 + 2;
        String resultName = "";
        for (int i= 0 ; i

直接调方法中getRandomName()方法就行。

你可能感兴趣的:(java后台开发,Java)