Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件从一个文件夹复制到另一个文件夹,并使用旧文件名重命名新文件夹中的文件+;时间戳_Java - Fatal编程技术网

Java 将文件从一个文件夹复制到另一个文件夹,并使用旧文件名重命名新文件夹中的文件+;时间戳

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

我想将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\\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