我想将word文档从一个文件夹复制到另一个文件夹。在新文件夹中,文件名应为oldFileName + timeStamp。 我到目前为止:
public static void main(String[] args) throws IOException {
File source = new File("C:\\Users\\rr\\test\\XYZ.docx");
File destination=new File("C:\\Users\\rr\\XYZ.docx");
FileUtils.copyFile(source,destination);
// copy from folder 'test' to folder 'rr'
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH-mm-ss");
String ts=sdf.format(source.lastModified());
String outFileName = destination.getName() + ts ;
//appending ts to the file name
System.out.println(" new file name is "+outFileName);
}
我可以将文件从文件夹test复制到文件夹rr,但文件名保持不变。如何将此新文件名更改为oldFileName + timeStamp?
答案 0 :(得分:1)
怎么样:
public static void main(String[] args) throws IOException {
File source = new File("C:\\Users\\rr\\test\\XYZ.docx");
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH-mm-ss");
String ts=sdf.format(source.lastModified());
File destination=new File("C:\\Users\\rr\\XYZ"+ts+".docx");
FileUtils.copyFile(source,destination);
System.out.println(" new file name is "+outFileName);
}
答案 1 :(得分:0)
首先创建文件的新名称,然后复制它......
不要忘记,您需要分隔名称和扩展名,以便在它们之间插入时间戳...
File source = new File("C:\\Users\\rr\\test\\XYZ.docx");
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH-mm-ss");
String ts = sdf.format(source.lastModified());
String name = source.getName();
String ext = name.substring(name.lastIndexOf("."));
name = name.substring(0, name.lastIndexOf("."));
String outFileName = name + " " + ts + ext;
//appending ts to the file name
System.out.println(" new file name is " + outFileName);
File destination = new File("C:\\Users\\rr", outFileName);
例如,这将在名为C:\Users\rr
的{{1}}中创建一个文件(我没有原始文件,因此日期为XYZ 01-01-1970 10-00-00.docx
)