开发者

How can we save a file with date?

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 = ne开发者_StackOverflow中文版w FileWriter("C:\\InjectionExcel"+ date1 +".csv");

date1 given is current date. But this code is not working. Where am I mistaking?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜