我正在寻找一种方法,在我的程序处理完文件后将旧文件转换为新文件。新文件在处理完后应该包含当前时间戳。例如,我的旧文件是test.txt
。处理完成后,应更改为test2017-10-13.txt
。我已经在互联网上寻找解决方案,但我仍然无法完成工作。这里是我当前的源代码
文件名不可更改
LocalDate now2 = LocalDate.now();
System.out.println("The current day is :" +now2);
File oldFile1 = new File("C:\\Users\\user\\Desktop\\test.txt");
File newFile1 = new File("C:\\Users\\user\\Desktop\\test"+now2+".txt");
boolean success = oldFile1.renameTo(newFile1);
System.out.println(success);
这是我的样本输出
The current day is :2017-10-13
false
它是一个已知的bug与Java?我在网上找到这个information。有没有办法做到这一点,而不需要从旧文件中复制内容并将其写入新文件?
你确定 “的test.txt” 的文件存在? – Jerry06
是的,它已经存在了,因为IOException没有提出 – test
不,你应该调用'file.exists()'来查看。如果它不存在,'rename'将返回false – Jerry06