Java - 生成随机字符串

时间:2020-02-23 14:35:01  来源:igfitidea点击:

在本教程中,我们将看到如何在Java中生成随机字符串。
有很多方法可以生成随机字符串.Let探索生成随机字符串的某些方法。

使用随机的简单Java代码

我们可以使用SecurerAndom类为我们生成随机字符串。

package org.igi.theitroad;
 
import java.security.SecureRandom;
 
public class RandomStringGeneratorMain {
 
	private static final String CHAR_LIST = 
			"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
	/**
	 * This method generates random string
	 * @return
	 */
	public String generateRandomStringUsingSecureRandom(int length){
		StringBuffer randStr = new StringBuffer(length);
		SecureRandom secureRandom = new SecureRandom();
		for( int i = 0; i < length; i++ ) 
			randStr.append( CHAR_LIST.charAt( secureRandom.nextInt(CHAR_LIST.length()) ) );
		return randStr.toString();
	}
 
 
	public static void main(String a[]){
		RandomStringGeneratorMain rsgm = new RandomStringGeneratorMain();
		System.out.println("Generating String of length 10: "+rsgm.generateRandomStringUsingSecureRandom(10));
		System.out.println("Generating String of length 10: "+rsgm.generateRandomStringUsingSecureRandom(10));
		System.out.println("Generating String of length 10: "+rsgm.generateRandomStringUsingSecureRandom(10));
		System.out.println("Generating String of length 8: "+rsgm.generateRandomStringUsingSecureRandom(8));
		System.out.println("Generating String of length 8: "+rsgm.generateRandomStringUsingSecureRandom(8));
		System.out.println("Generating String of length 8: "+rsgm.generateRandomStringUsingSecureRandom(8));
		System.out.println("Generating String of length 7: "+rsgm.generateRandomStringUsingSecureRandom(7));
		System.out.println("Generating String of length 7: "+rsgm.generateRandomStringUsingSecureRandom(7));
		System.out.println("Generating String of length 7: "+rsgm.generateRandomStringUsingSecureRandom(7));
	}
}

输出:

Generating String of length 10: Hz0hHRcO6X
Generating String of length 10: wSnjx6HNlv
Generating String of length 10: 4Wg9Iww0Is
Generating String of length 8: EdJmSrfC
Generating String of length 8: dAifHyQG
Generating String of length 8: HNnxieWg
Generating String of length 7: hQrqQ2L
Generating String of length 7: 0BWBtYI
Generating String of length 7: 3WStHON

使用apache common lang

我们可以使用Apache Common Lang来生成随机字符串。
很容易生成随机字符串,因为我们可以使用直转向API来创建随机字符串。

创建Alphanumeric的String.

我们可以使用randalStringUtils.randomalphanumeric方法生成字母数字随机strn = ing。

package org.igi.theitroad;
 
import org.apache.commons.lang3.RandomStringUtils;
 
public class ApacheRandomStringMain {
 
	public static void main(String[] args) {
		System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphanumeric(10));
		System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphanumeric(10));
		System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphanumeric(10));
	}
}

输出:

Generating String of length 10: Wvxj2x385N
Generating String of length 10: urUnMHgAq9
Generating String of length 10: 8TddXvnDOV

创建随机字母

我们可以使用randalstringutils.randomalphabetic方法生成字母数字随机strn = ing。

package org.igi.theitroad;
 
import org.apache.commons.lang3.RandomStringUtils;
 
public class ApacheRandomStringMain {
 
	public static void main(String[] args) {
		System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphabetic(10));
		System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphabetic(10));
		System.out.println("Generating String of length 10: "+RandomStringUtils.randomAlphabetic(10));
	}
}

输出:

Generating String of length 10: zebRkGDuNd
Generating String of length 10: RWQlXuGbTk
Generating String of length 10: mmXRopdapr