Unless you tell it to, Java won't treat it differently than any other file. The only drawback is that the filename is guaranteed to be at least 8 characters long minimum of 3 characters for the prefix, plus 5 or more characters generated by the function.
If that's too long for you, I suppose you could always just start with the filename "a", and loop through "b", "c", etc until you find one that doesn't already exist. There is a class org. RandomStringUtils that can be used to generate random strings of given length. Very handy not only for filename generation! Look at the File javadoc , the method createNewFile will create the file only if it doesn't exist, and will return a boolean to say if the file was created.
It will be guaranteed to be unique and shouldn't get too large unless you are pumping out a ton of files. Combining other answers, why not use the ms timestamp with a random value appended; repeat until no conflict, which in practice will be almost never. How about generate based on time stamp rounded to the nearest millisecond, or whatever accuracy you need If you store the last generated file name, you can append sequential letters or further digits to it as needed to make it unique. Or if you'd rather do it without locks, use a time step plus a thread ID, and make sure that the function takes longer than a millisecond, or waits so that it does.
It looks like you've got a handful of solutions for creating a unique filename, so I'll leave that alone. I would test the filename this way:. I understand that I am too late to reply on this question.
But I think I should put this as it seems something different from other solution. We can concatenate threadname and current timeStamp as file name. So we can remove special charater from thread name and then concatenate thread name and time stamp. Why not use synchronized to process multi thread. How are we doing?
Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is the best way to generate a unique and short file name in Java Ask Question. Asked 12 years, 8 months ago. Active 2 years, 2 months ago.
Viewed k times. Still not working im getting error — Fotic. Did you look at the links Andreas provided? The arguments must be Path objects, not Strings. Show 2 more comments. Active Oldest Votes. Use the new File NIO. Change the code to: import java. Andreas Andreas k 9 9 gold badges silver badges bronze badges.
Add a comment. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. It appears this is deprecated. Guava now recommends using java. Files to create a tmp dir. See here for more info. Edit: Actually, use System. Irfy Irfy 8, 1 1 gold badge 41 41 silver badges 63 63 bronze badges.
Bhisham Udasi Bhisham Udasi 11 1 1 bronze badge. Junaid Malik Junaid Malik 3 3 silver badges 12 12 bronze badges. The call to temp. Just check the return value of temp. Read the specification of createNewFile. The important word is atomic. Adding some explanation for the code would be helpful for the OP. Thank you for this code snippet, which might provide some limited short-term help.
A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions.
Please edit your answer to add some explanation, including the assumptions you've made. Sign up or log in Sign up using Google. Sign up using Facebook. It should delete on close without explicit call.
In the code sample on OutputStream close method call, temporary file is deleted. Does not matter whether error occurred or it run smoothly. How are we doing? Please help us improve Stack Overflow.
Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What is a safe way to create a Temp file in Java? Ask Question. Asked 7 years, 2 months ago. Active today.
0コメント