这是另一种用UUID生成随机字符串的方法。
1 public class RandomGenerator{ 2 private int length; 3 4 public void setLength(int length) { 5 this.length = length; 6 } 7 public RandomGenerator(int length){ 8 this.length=length; 9 } 10 public RandomGenerator(){ 11 this.length=32; 12 } 13 public String generate(){ 14 String s=UUID.randomUUID().toString(); 15 s=s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24); 16 return s.substring(0,length); 17 } 18 }