This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes append a file name with timestamp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "append a file name with timestamp" Watch "append a file name with timestamp" New topic
Author

append a file name with timestamp

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
I am trying to append a file name with time stamp so that every file will have a unique file name. I tried as below:



This works fine as the file name gets appended with 2008-04-07.txt Now I want to add timestamp with it to make it unique. I tried as below:
It is throwing error.
When I try to print the above piece of code, it prints as below:
But it is not allowing me to write a file as with that name filename_2008-04-07 10:48:05.083.txt (The parameter is incorrect) Is it due to the gap between date and time stamp. How do I overcome this? Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Are you using Windows ? In Windows Explorer, try to create a file whose name is the filename you have posted above. What does it say ?


[My Blog]
All roads lead to JavaRanch
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks for your response. I could finish it as below:



Now I could append the filename with date_time so that it each file name is unique.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Originally posted by Gopu Akraju:
Thanks for your response. I could finish it as below:



Now I could append the filename with date_time so that it each file name is unique.

You can use one single method as well:

It will be just a bit more efficient - only one SimpleDateFormat and Date object for each file, as well as no String concatenation.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: append a file name with timestamp
 
Similar Threads
Search on a string
Appending Date and TimeStamp to the File name
add timestamp with .txt file & write some information to the .txt file
write into a file?
xpath problem