简体   繁体   English

将文件从一个目录复制到另一个目录,并使用时间戳附加新文件,而不是用Java覆盖

[英]Copy files from one directory to another and append new files with timestamp instead of overwriting in Java

I want to copy files from source directory to destination. 我想将文件从源目录复制到目标。 If the file already exists in the destination directory, then append the new file to be copied with its timestamp so that there is no overwrite. 如果该文件已存在于目标目录中,则将要复制的新文件及其时间戳附加在文件上,以免覆盖。 How do I check for duplicates and append timestamp to the new file name? 如何检查重复项并将时间戳附加到新文件名? Please help! 请帮忙!

public static void copyFolder(File src, File dest)
    throws IOException{
        //list all the directory contents
        String files[] = src.list();
        for (String file : files) {
           //construct the src and dest file structure
           File srcFile = new File(src, file);
           File destFile = new File(dest, file);
           //recursive copy
           copyFolder(srcFile,destFile);
        }
    }else{
        //if file, then copy it
        //Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
        int length;
            //copy the file content in bytes
            while ((length = in.read(buffer)) > 0){
               out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
    }
}

You can check if the file exists using File.exist() method, if exists, you can open the file in the append mode 您可以使用File.exist()方法检查文件是否存在,如果存在,则可以以追加模式打开文件

The code is something like this 代码是这样的

File f = new File(oldName);
if(f.exists() && !f.isDirectory()) { 
    long currentTime=System.currentTimeMillis();
    String newName=oldName+currentTime;
    // do the copy

}
    //construct the src and dest file structure
    File srcFile = new File(src, file);
    File destFile = new File(dest, file);
    while (destFile.exists()) {
        destFile = new File(dest, file + '-' + Instant.now());
    }

In one case the destination file got named test-file.txt-2018-03-14T11:05:21.103706Z . 在一种情况下,目标文件名为test-file.txt-2018-03-14T11:05:21.103706Z The time given is in UTC. 给定的时间以UTC为单位。 In any case you will end up with a name of file that doesn't already exist (if the loop terminates, but I have a hard time seeing the scenario where it doesn't). 无论如何,您最终都会得到一个尚不存在的文件名(如果循环终止,但是我很难看到它不存在的情况)。

You may want to append the timestamp only to plain files and reuse existing folders (directories), I don't know your requirements here. 您可能只想将时间戳附加到纯文件并重用现有的文件夹(目录),在这里我不知道您的要求。 And you may want to append the timestamp before the extension if there is one (to get test-file-2018-03-14T11:05:21.103706Z.txt instead). 如果有一个时间戳,您可能希望在扩展名之前附加时间戳(以获取test-file-2018-03-14T11:05:21.103706Z.txt代替)。 I trust you to make the necessary modifications. 我相信您可以进行必要的修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM