简体   繁体   English

Java文件副本 - 如何继承目标目录的权限

[英]Java File copy - how to inherit permissions of destination directory

When I copy a file from source folder to destination folder the permissions of the destination folder are not inherited by newly copied file. 将文件从源文件夹复制到目标文件夹时,新复制的文件不会继承目标文件夹的权限。 is there a way in Java where file copy to destination would inherit permissions of destination folder? 在Java中有一种方法,文件复制到目标将继承目标文件夹的权限?

In java7 - You can do the following 在java7中 - 您可以执行以下操作

Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES)

I have not tested this code. 我还没有测试过这段代码。 Hope it helps!! 希望能帮助到你!!

Using Files.Copy works fine and I tested this code when renameTo() or FileInput/OutputStream code didn't work. 使用Files.Copy工作正常,我在renameTo()或FileInput / OutputStream代码不起作用时测试了这段代码。

Try without the StandardCopyOption and the permissions of the destination folder are automatically inherited like this: Files.copy(source.toPath(), destination.toPath()); 尝试不使用StandardCopyOption,并自动继承目标文件夹的权限,如下所示: Files.copy(source.toPath(), destination.toPath());

Hope it can help. 希望它可以提供帮助。

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

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