Reputation: 13
How can we save file with current date?
Date date11 = Calendar.getInstance().getTime();
DateFormat formatter =new SimpleDateFormat("d/M/yyyy");
String date1 =formatter.format(date11);
FileWriter fw = new FileWriter("C:\\InjectionExcel"+ date1 +".csv");
date1
given is current date. But this code is not working. Where am I mistaking?
Upvotes: 0
Views: 980
Reputation: 1109685
A file name can't contain any of the following characters in Windows:
\ / * ? " < > |
Your problem is caused by attempting to use /
as file name. It will be interpreted as path separator. For example, if the current day is 23 and the directory C:\InjectionExcel23
does not exist, then you will get something like the following exception (which you should initially have reported in your question!):
java.io.IOException: The system cannot find the path specified
Unrelated to the concrete problem, the way how you created the today's date is clumsy. You're generating all that unnecessary Calendar
overhead. Just use new Date()
.
Date date11 = new Date();
Upvotes: 2