Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create Unique File names 3

Status
Not open for further replies.

Tony220

Programmer
Apr 18, 2004
17
US
Hi,
I Would like my servlet to save a string to a "dump/temp" folder every time it runs. (Backup and debugging purposes)

How do I create a safe file name that is not already there? When using Multipart form upload there is the Default renaming policy that increments the file name if neccessary.
(ie if test.txt already exists this becomes test1.txt if that already exists test2.txt etc..)

Any suggestions?
Thanks a load.

 
hi

what about time as file name? for example: 19.04.2004 ... .

to how deep you want. hour, minute, second etc.
 
If from a servlet/JSP use :

String filename = request.getSession().getId() +"_" +System.currentTImeMillis() +".txt";
 
Times can be problematic on a busy system. For example if you are serving three or four hundred transactions per second, especialy on multi-engined CPUs, you can easily get a collision on the file name. sedj's solution is much neater as it uses the session id as well.
 
you can try something like

while(exist(filename)) {filename = basename + next;next++;}
create (filename);
it is a pseudocode, so translate it in java

Ion Filipski
1c.bmp
 
When using times for filenames, I always recommend:

YYYY-MM-dd-hh-mm-ss

as the format. It makes finding the most recent much simpler.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top