我们如何保存带有日期的文件?

发布于 2024-12-06 15:43:28 字数 317 浏览 2 评论 0原文

我们如何保存当前日期的文件?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凯凯我们等你回来 2024-12-13 15:43:28

在 Windows 中,文件名不能包含以下任何字符:

\ / * ? " < > |

您的问题是由于尝试使用 / 作为文件名引起的。它将被解释为路径分隔符。例如,如果当前日期是 23 且目录 C:\InjectionExcel23 不存在,那么您将得到类似以下异常的信息(您最初应该在问题中报告该异常!):

java.io.IOException:系统找不到指定的路径


,您创建今天日期的方式很笨拙。您正在生成所有不必要的日历开销。只需使用new Date()即可。

Date date11 = 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 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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文