我们如何保存带有日期的文件?
我们如何保存当前日期的文件?
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
是当前日期。但这段代码不起作用。我哪里弄错了?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Windows 中,文件名不能包含以下任何字符:
您的问题是由于尝试使用
/
作为文件名引起的。它将被解释为路径分隔符。例如,如果当前日期是 23 且目录C:\InjectionExcel23
不存在,那么您将得到类似以下异常的信息(您最初应该在问题中报告该异常!):,您创建今天日期的方式很笨拙。您正在生成所有不必要的
日历
开销。只需使用new Date()
即可。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 directoryC:\InjectionExcel23
does not exist, then you will get something like the following exception (which you should initially have reported in your question!):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 usenew Date()
.