通过添加当前日期重命名文件
renaming a file by adding current date
我正在处理一个项目,其中一部分是将文件重命名为 "Filename + currentDate"。我一直在寻找答案并找到了一些关于该问题的有用主题,但我仍然没有让我的代码正常工作(或根本没有做任何事情)。
这是我的代码:
File oldTxt = new File("Filename.txt");
GregorianCalendar now = new GregorianCalendar();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String archivedName = "Filename"+
String.valueOf(dateFormat.format(now.getTime())).replace(".", "_")+".txt";
File archivedTxt = new File(archivedName);
oldTxt.renameTo(archivedTxt);
非常感谢任何帮助。
您错过了创建新文件的行。
oldTxt.createNewFile();
File oldTxt = new File("Filename.txt");
--> oldTxt.createNewFile();
GregorianCalendar now = new GregorianCalendar();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String archivedName = "Filename"+
String.valueOf(dateFormat.format(now.getTime())).replace(".", "_")+".txt";
File archivedTxt = new File(archivedName);
oldTxt.renameTo(archivedTxt);
我正在处理一个项目,其中一部分是将文件重命名为 "Filename + currentDate"。我一直在寻找答案并找到了一些关于该问题的有用主题,但我仍然没有让我的代码正常工作(或根本没有做任何事情)。
这是我的代码:
File oldTxt = new File("Filename.txt");
GregorianCalendar now = new GregorianCalendar();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String archivedName = "Filename"+
String.valueOf(dateFormat.format(now.getTime())).replace(".", "_")+".txt";
File archivedTxt = new File(archivedName);
oldTxt.renameTo(archivedTxt);
非常感谢任何帮助。
您错过了创建新文件的行。
oldTxt.createNewFile();
File oldTxt = new File("Filename.txt");
--> oldTxt.createNewFile();
GregorianCalendar now = new GregorianCalendar();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
String archivedName = "Filename"+
String.valueOf(dateFormat.format(now.getTime())).replace(".", "_")+".txt";
File archivedTxt = new File(archivedName);
oldTxt.renameTo(archivedTxt);