How to create a guaranteed unique name in Java, file name

Source: Internet
Author: User
Tags uuid

------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------

This blog has to say, how to create a unique file name , create a unique string string Why create a unique ? In many cases, such as file upload , filename should be inconsistent , otherwise covered out , how inconsistent, this is the content of this blog to speak

Note: It is foolish to use random numbers directly, and it will have the same number in a very large probability . Way One: time stamp

Recommended index:★

Cause: timestamps may occur in high concurrency scenarios with the same

Solution: re-spell the random number, pay attention to stitching when you don't let two do addition time stamp variable + "" + Random number variable

        // time stamp the first kind        System.out.println (new  Date (). GetTime ());         // time stamp The second kind        System.out.println (System.currenttimemillis ());

  Random number :

        // generate six-bit random numbers        SYSTEM.OUT.PRINTLN ((int) ((Math.random () *9+1) *100000));
mode two: microsecond number

Recommended index:

Reason: It is more granular than time stamp and has stronger control

  You can also add random numbers or join your own rules.

        // number of microseconds        System.out.println (System.nanotime ());
Way Three:UUID

Recommended INDEX: ★ ★

Cause: He's a java.util under the bag. A class that can produce different character variables, it's very powerful.

        // the method of using UUID to                    import Java.util.UUID;        UUID Randomuuid = uuid.randomuuid ();        System.out.println (randomuuid);
mode four: customize the rules on top three basis

Recommended INDEX: ★ ★

Reason: In the daily three kinds are enough to meet their own small demo, small project use, but in the mature big project, perhaps some not enough to see, so, I said fourth kind

Idea: The file name is not a string, do the three sides of the stitching three ways any kind of time , you can add their own rules, such as the uploader ID, such as you upload from the explanation, even add 8 or more random numbers, etc.

is to customize the rules to make the probabilities smaller !!!!!!!!!

How to create a guaranteed unique name in Java, file name

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.