Java 将文件从一个文件夹复制到另一个文件夹,并使用旧文件名重命名新文件夹中的文件+;时间戳
我想将word文档从一个文件夹复制到另一个文件夹。在新文件夹中,文件名应为oldFileName+时间戳。 到目前为止,我已经做到了:Java 将文件从一个文件夹复制到另一个文件夹,并使用旧文件名重命名新文件夹中的文件+;时间戳,java,Java,我想将word文档从一个文件夹复制到另一个文件夹。在新文件夹中,文件名应为oldFileName+时间戳。 到目前为止,我已经做到了: 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\\X
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+时间戳?关于:
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);
}
首先创建文件的新名称,然后复制它 别忘了,你需要把名字和分机分开,这样你就可以在它们之间插入时间戳
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
中创建一个名为XYZ 01-01-1970 10-00-00.docx的文件(我没有原始文件,因此日期为0
)