aspose file tools*
The moose likes Beginning Java and the fly likes How to create the unique file name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to create the unique file name?" Watch "How to create the unique file name?" New topic
Author

How to create the unique file name?

sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Every time when the client programs runs, it has to create a file to log the message. If it creates a file with the name say, "LogMsg", then sometimes from the same machine, if two requests are shooted, then the contents of that file will be overwritten or it can be appended (for each request).
Instead of that, i want to create a file everytime with the "unique name" so that the log message will be there in that file only for that particular request. For each request, i want to create one file with the unique name.
Could anyone tell how to create a unique file name every time?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
One solution is to append a random number at the end of the filename like for instance LogMsg563547728 or something like that using Math.random().
Another solution, which in my opinion is the simplest, is to append the current Time in milliseconds to "LogMsg".
"LogMsg"+new Date().getTime().
This way you are sure to get different files every milliseconds
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
Hi Valentin Crettaz,
Thank you very much...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create the unique file name?