java生成4位不重复字符(包含大写字母、小写字母、数字)
2016-03-18 15:13
711 查看
需要生成4位不重复的字符作为唯一参数,要求只能使用大写字母、小写字母和数字的组合。大写字母26个、小写字母26个、10个数字共62个字符。不重复排列个数:62*62*62*62=14776336个。
Java高级群1群:224651178
[b]Java高级群1群:134787504[/b]
/** * * @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
[b]Java高级群1群:134787504[/b]
相关文章推荐
- Spring Security(01)——初体验
- java试题(Servlet)
- JAVA_SE基础——59.权限访问修饰符
- java异步计算Future的使用
- Eclipse android 调试技巧
- 使用Java7提供的Fork/Join框架
- 如何在Eclipse中查看JDK以及JAVA框架的源码(转载)
- Java动态代理JDK与Cglib
- (1)java自带软件包javax.crypto的使用方法,对称加密方法和非对称加密方法
- Java enum的用法详解
- Spring 的优秀工具类盘点,第 1 部分: 文件资源操作和 Web 相关工具类
- 阿里云服务器搭建JavaWeb项目环境
- java入门---类,构造函数,对象。
- Spring集成log4j日志管理
- Log4j学习笔记(2)_log4j配置示例&Spring集成log4j
- eclipse里面的@SuppressWarnings({ "unchecked", "rawtypes" })
- java中ServletContext的作用和常见获取方法
- java画棋盘
- Mybatis学习记录(八)----Mybatis整合Spring
- spring mvc 透传