简体   繁体   English

Java:将文件从一个目录复制/移动到另一个目录的安全方法

[英]Java: Safe way to copy/move files from one directory to another

We have a system where we are dealing with file operations, and in my case i would have to copy or move file(s) from one directory to another. 我们有一个处理文件操作的系统,以我为例,我将不得不将文件从一个目录复制或移动到另一个目录。 Could anybody let me know a safe way to do this? 有人能让我知道安全的方法吗? For instance: lets say during the copy or move execution, if the system reboots, how do i ensure my operation executes gracefully. 例如:假设在复制或移动执行期间,如果系统重新启动,我如何确保我的操作正常执行。 An example snippet would be very helpful. 一个示例代码片段将非常有帮助。

You can use this way: 您可以使用这种方式:

File fileFrom= new File("/FileDirectory");
Path FROM = Paths.get(fileFrom.getPath());
File fileTo= new File("/FileDirectory");
Path TO = Paths.get(fileTo.getPath());

        //sobreescribir el fichero de destino, si existe, y copiar los atributos, incluyendo los permisos rwx
        CopyOption[] options = new CopyOption[]{
            StandardCopyOption.REPLACE_EXISTING,
            StandardCopyOption.COPY_ATTRIBUTES
        };
        try {
            Files.copy(FROM, TO, options);
        } catch (FileNotFoundException ex) {
            System.out.println("Error1: " + ex.getMessage());
        } catch (IOException ex) {
            System.out.println("Error2: " + ex.getMessage());
        }

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

相关问题 Java中如何将特定文件从一个目录复制到另一个目录 - How to copy specific files from one directory to another in Java 将文件从一个目录复制到另一个目录,并使用时间戳附加新文件,而不是用Java覆盖 - Copy files from one directory to another and append new files with timestamp instead of overwriting in Java 在Java中将文件从一个目录复制到另一个目录 - Copying files from one directory to another in Java 如何将所有文件从一个目录移动并覆盖到另一个目录? - How to move and overwrite all files from one directory to another? 番石榴如何将所有文件从一个目录复制到另一个目录 - Guava how to copy all files from one directory to another 将文件从一个目录复制到另一个目录而无需替换 - Copy files from one directory to another without replacement 如何在Java中将文件从一个驱动器移动到另一个驱动器 - How to move files from one drive to another drive in java Java - 将所有文件从一个目标移动并重命名为另一个目标 - Java - Move and Rename all files from one destination to another 将目录,子目录和文件从一台服务器复制到Java中的另一台服务器 - Copy directories, subdirectory and files from one server to another server in java 如何使用Java将文件从一个文件夹复制到另一个文件夹? - How to copy files from one folder to another using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM