java随机生成字符串(字符随机生成类 生成随机字符组合)

代码如下:

package com.phyl.password;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
/**
 * 字符随机生成类
 * @author ASUS
 *
 */
public class PassWord {

/**
  * 密码类型枚举
  * @author ASUS
  */
 public static enum TYPE {
  /**
   * 字符型
   */
  LETTER,
  /**
   * 大写字符型
   */
  CAPITAL,
  /**
   * 数字型
   */
  NUMBER,
  /**
   * 符号型
   */
  SIGN,
  /**
   * 大+小字符 型
   */
  LETTER_CAPITAL,
  /**
   * 小字符+数字 型
   */
  LETTER_NUMBER,
  /**
   * 大+小字符+数字 型
   */
  LETTER_CAPITAL_NUMBER,
  /**
   * 大+小字符+数字+符号 型
   */
  LETTER_CAPITAL_NUMBER_SIGN
 }

private static String[] lowercase = {
   "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"};

private static String[] capital = {
   "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"};

private static String[] number = {
   "1","2","3","4","5","6","7","8","9","0"};

private static String[] sign = {
   "~","!","@","#","$","%","^","&","*","(",")","_","+","`","-","=",
   "{","}","|",":","\"","<",">","?",
   "[","]","\\",";","'",",",".","/"};

/**
  * 静态随机数
  */
 private static Random random = new Random();

public static void main(String[] args) {
  System.out.println(PassWord.getRandom(24, PassWord.TYPE.CAPITAL));
 }

/**
  * 获取随机组合码
  * @param num 位数
  * @param type 类型
  * @type
  * <br>字符型 LETTER,
  * <br>大写字符型 CAPITAL,
  * <br>数字型 NUMBER,
  * <br>符号型 SIGN,
  * <br>大+小字符型 LETTER_CAPITAL,
  * <br>小字符+数字 型 LETTER_NUMBER,
  * <br>大+小字符+数字 型 LETTER_CAPITAL_NUMBER,
  * <br>大+小字符+数字+符号 型 LETTER_CAPITAL_NUMBER_SIGN
  */
 public static String getRandom(int num,TYPE type){
  ArrayList<String> temp = new ArrayList<String>();
  StringBuffer code = new StringBuffer();
  if(type == TYPE.LETTER){
   temp.addAll(Arrays.asList(lowercase));
  }else if(type == TYPE.CAPITAL){
   temp.addAll(Arrays.asList(capital));
  }else if(type == TYPE.NUMBER){
   temp.addAll(Arrays.asList(number));
  }else if(type == TYPE.SIGN){
   temp.addAll(Arrays.asList(sign));
  }else if(type == TYPE.LETTER_CAPITAL){
   temp.addAll(Arrays.asList(lowercase));
   temp.addAll(Arrays.asList(capital));
  }else if(type == TYPE.LETTER_NUMBER){
   temp.addAll(Arrays.asList(lowercase));
   temp.addAll(Arrays.asList(number));
  }else if(type == TYPE.LETTER_CAPITAL_NUMBER){
   temp.addAll(Arrays.asList(lowercase));
   temp.addAll(Arrays.asList(capital));
   temp.addAll(Arrays.asList(number));
  }else if(type == TYPE.LETTER_CAPITAL_NUMBER_SIGN){
   temp.addAll(Arrays.asList(lowercase));
   temp.addAll(Arrays.asList(capital));
   temp.addAll(Arrays.asList(number));
   temp.addAll(Arrays.asList(sign));
  }
  for (int i = 0; i < num; i++) {
   code.append(temp.get(random.nextInt(temp.size())));
  }
  return code.toString();
 }

}

(0)

相关推荐

  • Java实现产生随机字符串主键的UUID工具类

    本文实例讲述了Java实现产生随机字符串主键的UUID工具类.分享给大家供大家参考,具体如下: package com.gcloud.common; import java.net.InetAddress; import java.util.UUID; /** * uuid工具类 * Created by charlin on 2017/9/9. */ public class UUIDUtil { private String sep = ""; private static int

  • Java 生成随机字符串数组的实例详解

    Java 生成随机字符串数组的实例详解 利用Collections.sort()方法对泛型为String的List 进行排序.具体要求: 1.创建完List<String>之后,往其中添加十条随机字符串 2.每条字符串的长度为10以内的随机整数 3.每条字符串的每个字符都为随机生成的字符,字符可以重叠 4.每条随机字符串不可重复 将涉及到的知识有: String.StringBuffer.ListArray.泛型.Collections.sort.foreach.Random等相关知识,算是

  • Java 随机取字符串的工具类

    一.Java随机数的产生方式 在Java中,随机数的概念从广义上将,有三种. 1.通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字. 2.通过Math.random()返回一个0到1之间的double值. 3.通过Random类来产生一个随机数,这个是专业的Random工具类,功能强大. 二.Random类API说明 1.Java API说明 Random类的实例用于生成伪随机数流.此类使用 48 位的种子,使用线性同余公式对其进行修改(请参阅 D

  • JAVA获得包含0-9、a-z、A-Z范围内字符串的的随机数实例

    一.获得0-9,a-z,A-Z范围的随机字符串 复制代码 代码如下: /** * JAVA获得0-9,a-z,A-Z范围的随机数 * @param length 随机数长度 * @return String */ public static String getRandomChar(int length) { char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',

  • Java实现的3des加密解密工具类示例

    本文实例讲述了Java实现的3des加密解密工具类.分享给大家供大家参考,具体如下: package com.gcloud.common; import org.apache.poi.poifs.property.Child; import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax

  • java生成随机数(字符串)示例分享

    用来生成简单的随机java生成随机数,大小+数字.没特符 复制代码 代码如下: package passwords;import java.util.Random;public class pwdGen { private Random rdseed=new Random(); /**  *@param  *length  password length;  *@param  *letters  boolean non-capital letters combination control; 

  • Java生成含字母和数字的6位随机字符串

    本文实例为大家分享了Java生成6位随机字符串的实现代码,具体内容如下 package com.amos.tools; import java.util.ArrayList; import java.util.List; import java.util.Random; public class InvertCodeGenerator { public static void main(String[] args) { // TODO Auto-generated method stub Lis

  • java实现随机生成UUID

    java实现随机生成UUID public class IDGenerator { private static long num=0; /** * 随机生成UUID * @return */ public static synchronized String getUUID(){ UUID uuid=UUID.randomUUID(); String str = uuid.toString(); String uuidStr=str.replace("-", ""

  • java实现无符号数转换、字符串补齐、md5、uuid、随机数示例

    复制代码 代码如下: package com.hongyuan.test; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.Random;import java.util.UUID; /* * 杂项(无符号数转换,字符串补齐,md5,uuid,随机数) */public class SundryTest { //转成无符号数 public stati

  • java生成随机字符串的两种方法

    本文实例为大家分享了java生成随机字符串的具体代码,供大家参考,具体内容如下 import java.util.Random; public class CharacterUtils { //方法1:length为产生的位数 public static String getRandomString(int length){ //定义一个字符串(A-Z,a-z,0-9)即62位: String str="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZ

  • java随机字符串生成示例

    复制代码 代码如下: package com.phyl.password; import java.util.ArrayList;import java.util.Arrays;import java.util.Random;/** * 字符随机生成类 * @author ASUS * */public class PassWord { /**  * 密码类型枚举  * @author ASUS  */ public static enum TYPE {  /**   * 字符型   */  L

随机推荐