I hope you could help me, I'm trying to call in the date from another class and looks like "2011-03-09 06-57-40", I want to use this to create the file below but everytime I do when the output runs it creates a new file as it re-runs calling the dat(). I know what's going wrong I'm just not sure how to fix it, I want to permently writw to the same file. I hope this makes sense? :/
Thank you for any help in advance :)
date d = new date(); String cdate = d.date(); String f = h; try{ PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(cdate + ".tsv", true))); out.print(f); out.print("\t"); out.close(); }catch (IOException e){ }
I'd use YYYY-MM-DD HHmmss for filenames, unless there is a particular need for timezones or a possible need to parse them into ISO dates; in those cases an ISO date would probably be preferrable.
First, import the module and then get the current time with datetime. now() object. Now convert it into a string and then create a file with the file object like a regular file is created using file handling concepts in python.
To create a file named the current date/time:
Date date = new Date() ; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; File file = new File(dateFormat.format(date) + ".tsv") ; BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write("Writing to file"); out.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With