欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JAVA-UUID篇

程序员文章站 2022-03-01 15:12:50
import java.util.Random;import java.util.UUID; /** * @author Weirdo-world * */public class DemoUUID { public static void main(String[] args) { String[ ......

import java.util.Random;
import java.util.UUID;

/**
* @author Weirdo-world
*
*/
public class DemoUUID {

  public static void main(String[] args) {
    String[] ext = new String[] { ".jpg", ".gif", ".png", ".rar" };
    Random rand = new Random();
    for (int i = 0; i < 5; i++) {
      String id = UUID.randomUUID().toString(); //不去掉-
      System.out.println(id + ext[rand.nextInt(ext.length)]);
    }
    System.out.println();
    for (int i = 0; i < 5; i++) {
      String ids = UUID.randomUUID().toString().replace("-", ""); //去掉-
      System.out.println(ids + ext[rand.nextInt(ext.length)]);
    }
  }
}

 

 

运行结果:

44c042f9-fa67-4559-8d16-a52588f9f25b.png
a97b7c39-a5b2-4eb9-8e15-31df9542232d.gif
3adf14b3-3a06-48c4-bb40-7e3987316f3c.gif
8997e9c2-adda-4064-bf71-c9c7c9f869a0.rar
bb97952c-e579-40b1-935f-cb3392396dbc.gif

2bc64134344f47658cef4d3faa0700c9.rar
9cbeb6e7232e4f7aabc01d4a9c395702.png
2246f05fe42e4c93818c98f7c072db2b.gif
539333537e2f43f6a2f1fecf494d76a3.gif
adae08d072664e7e8d76a072e624c78e.png