java生成4位不重复字符(包含大写字母、小写字母、数字)

需要生成4位不重复的字符作为唯一参数,要求只能使用大写字母、小写字母和数字的组合。大写字母26个、小写字母26个、10个数字共62个字符。不重复排列个数:62*62*62*62=14776336个。

/**
 * 
 * @author wzp
 * 2016-3-18
 */
public class DmSequenceUtil {

	static String bwords[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i",
			"j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
			"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
			"9" };
	static Random random = new Random();
	static int index1 = 0;
	static int index2 = 0;
	static int index3 = 0;
	static int index4 = 0;
	static boolean isRead = true;

	/**
	 * 支持14776336个不重复4位字符串 2016-3-18
	 * 
	 * @return
	 * @throws Exception
	 */
	public static synchronized String getNoRepeatId() throws Exception {
		// 判断是否需要读取文件记录
		if (isRead) {
			String sequence = read();
			if (sequence != null && sequence.length() == 4) {
				index1 = getIndex(String.valueOf(sequence.charAt(0))) + 10;
				index2 = getIndex(String.valueOf(sequence.charAt(1))) + 10;
				index3 = getIndex(String.valueOf(sequence.charAt(2))) + 10;
				index4 = getIndex(String.valueOf(sequence.charAt(3))) + 10;
			}
			isRead = false;
		}

		getRandom();
		String id = "" + bwords[index1 == 0 ? 0 : index1 - 1]
				+ bwords[index2 == 0 ? 0 : index2 - 1]
				+ bwords[index3 == 0 ? 0 : index3 - 1] + bwords[index4 - 1];
		write(id);
		return id;

	}

	private static int getIndex(String word) {
		for (int i = 0; i < bwords.length; i++) {
			if (bwords[i].equals(word)) {
				return i;
			}
		}
		return 0;
	}

	private static void getRandom() throws Exception {
		if (index4 < 62) {
			index4++;
		} else {
			index4 = 1;
			if (index3 < 62) {
				index3++;
			} else {
				index3 = 1;
				if (index2 < 62) {
					index2++;
				} else {
					index2 = 1;
					if (index1 < 62) {
						index1++;
					} else {
						throw new Exception("结束");
					}
				}
			}
		}

	}

	private static void write(String sequence) throws IOException {
		File file = new File("sequence.dat");
		FileWriter fw = new FileWriter(file, false);
		fw.write(sequence);
		fw.flush();
		fw.close();
	}

	private static String read() throws IOException {
		String sequence = "";
		File file = new File("sequence.dat");
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			sequence = br.readLine();
			br.close();
			fr.close();
		} catch (Exception e) {
			System.err.println("读取文件出错");
		}
		return sequence;
	}

}



Java高级群1群:224651178

Java高级群1群:134787504






好的,以下是用Java(8)随机生成指定长度及参数要求的验证码的代码: ``` import java.util.ArrayList; import java.util.List; import java.util.Random; public class VerificationCodeGenerator { private static final String DIGITS = "0123456789"; private static final String LOWER_CASE_LETTERS = "abcdefghijklmnopqrstuvwxyz"; private static final String UPPER_CASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static String getCode(int len, boolean isDigit, boolean isLowerCase, boolean isUpperCase, boolean isRepeat) { if (!isDigit && !isLowerCase && !isUpperCase) { throw new IllegalArgumentException("At least one of isDigit, isLowerCase and isUpperCase must be true"); } if (len <= 0) { throw new IllegalArgumentException("len must be greater than 0"); } Random random = new Random(); List<Character> chars = new ArrayList<>(); if (isDigit) { for (int i = 0; i < DIGITS.length(); i++) { chars.add(DIGITS.charAt(i)); } } if (isLowerCase) { for (int i = 0; i < LOWER_CASE_LETTERS.length(); i++) { chars.add(LOWER_CASE_LETTERS.charAt(i)); } } if (isUpperCase) { for (int i = 0; i < UPPER_CASE_LETTERS.length(); i++) { chars.add(UPPER_CASE_LETTERS.charAt(i)); } } if (!isRepeat && chars.size() < len) { throw new IllegalArgumentException("Not enough characters to generate a unique code"); } StringBuilder codeBuilder = new StringBuilder(len); for (int i = 0; i < len; i++) { int index = random.nextInt(chars.size()); codeBuilder.append(chars.get(index)); if (!isRepeat) { chars.remove(index); } } return codeBuilder.toString(); } } ``` 调用方式示例: ``` String code = VerificationCodeGenerator.getCode(6, true, true, true, false); System.out.println(code); ``` 该示例调用方法生成一个长度为6的验证码,它包含数字小写字母大写字母,但不允许有重复字符出现。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值