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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timestamping

Status
Not open for further replies.

cajchris

Programmer
Oct 13, 2005
57
GB
is there any way i can timestamp a file name, so as to avoid conflict in files with the same name. I am implementing a forum attachment upload facility and want to ensure no uploads share the same name.

regards
cajchris
 
Sure, use System.currentTimeMillis() and add the result to the filename.

Cheers,
Dian
 
I'm guessing you are using JSP/servlets for this forum ...

If that is the case, I would use a timestamp (ie Dian's answer) combined with the session id. Else you could get two people uploading a file at exactly the same time (unlikely admittedly).

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
cheers dian, just what im looking for

cajchris
 
Errmmm... I really would watch out for duplicate file names you know ... using a time and session id would avoid this.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I would never ever expect two users to upload the same file at the same millisecond.

Cheers,
Dian
 
You may not expect it, but it doesn't mean it can't happen - and you certainly should not programme on the principle of "oh well, it will probably be OK" !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
My opinion is that you should design and code to face actual requirements, not imaginary or remote ones. If you want to take into account all the possibilities, even the impossible ones, your design will be too complicated and your implementation unclear.

I know of some production systems, highly loaded, that use that approach with no problem.

I would definitely not write a single line of code for this case.

Cheers,
Dian
 
So to stop it, I'd code like this :p

Code:
/**
 * Function that adds two numbers
 * @author: sedj
 **/

public int add(int a, int b) {

 if ((a>b)&&(b>a))
    throw new sedj.exceptions.ImpossibleNumberException();

 if ((a>Integer.MAX_VALUE)||(b>Integer.MAX_VALUE))
    throw new sedj.exceptions.NumberGreaterThanInfiniteException();

 if ((a<Integer.MIN_VALUE)||(b<Integer.MIN_VALUE))
    throw new sedj.exceptions.NumberLesserThanMinusInfiniteException();

 try {
   int c = a + b;
 } catch (sedj.exceptions.EndOfWorldException EOWe) { 
   System.out.println("The world has ended: your result may be innacurate")
   EOWe.printStackTrace();
 }
 return c;
}

Cheers,
Dian
 
[/code]
try {
int c = a + b;
} catch (sedj.exceptions.EndOfWorldException EOWe) {
System.out.println("The world has ended: your result may be innacurate")
EOWe.printStackTrace();
}
return c;
[/code]
Will not compile. Unknown 'c'.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top