程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java隨機生成字符串(字符隨機生成類 生成隨機字符組合)

java隨機生成字符串(字符隨機生成類 生成隨機字符組合)

編輯: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();
 }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved