Create folder with timestamp

Dear all,

I'm using FileWriter to create a file. I want to create a folder in the outputPath with the timestamp and write the file to this location.

I'm not sure how to go about this.
Thanks

String outputFilename = "result";
String outputPath = "output/";

Writer writer = new FileWriter(outputPath + outputFilename + i + ".csv", true);

Open in new window

AndyC1000Asked:
Who is Participating?
 
CEHJCommented:
The only problem is that some of the characters in a timestamp are not particularly filesystem-friendly.  You can use java.sql.Timestamp but with suitable replacement:

       String timestamp = new Timestamp(System.currentTimeMillis()).toString();
        timestamp = timestamp.replaceAll("[ :.]", "_");

        File d = new File(timestamp);
        d.mkdir();

Open in new window

0
 
Jeffrey Dake Senior Director of TechnologyCommented:
You could use what CEHJ has recommended, however how exactly do you want the directories created. Are you looking for one directory per day?  If so you use Calendar with a date formater to format what you are trying to do.

This would probably be more useful then a new directory with the millisecond in it. That is not very human readable for the future.

Hope that helps.
0
 
CEHJCommented:
There are actually more important considerations than readability: for instance, why would you want a folder with a timestamp as its name?
Are you putting more than one file into it? If so, then you're likely to need a coarser granularity than a timestamp with milliseconds or possibly even seconds (maybe even coarser)
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
AndyC1000Author Commented:
The purpose of the folder is to store the date/time of the model run, I will need to record time down to seconds.
0
 
CEHJCommented:
timestamp = timestamp.replaceAll("[ :.]", "_").replaceAll("_\\d+$", "");

Open in new window


(that's importing java.sql.Timestamp) or use SimpleDateFormat as the above code is getting verbose
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.