------------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