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.

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

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

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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)
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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.
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.