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?
Asked
Active
Viewed 5,277 times
6
-
Which operating system? What permissions exactly are you referring to? – Duncan Jones Dec 05 '14 at 08:59
-
Windows. I am referring to security permissions like which user can access file and how much access control(read/write etc.) one has – Sandy Dec 05 '14 at 09:04
-
Such a basic question. I'm looking for a solution as well. :/ – tresf Aug 20 '22 at 05:34
2 Answers
3
In java7 - You can do the following
Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES)
I have not tested this code. Hope it helps!!

Ouney
- 1,164
- 1
- 10
- 22
-
2actually my issue not to copy file permission but to inherit it from the destination folder. – Sandy Dec 05 '14 at 09:42
-
3`COPY_ATTRIBUTES` doesn't work on all file systems. On a macOS, it throws `UnsupportedOperationException`. – Abhijit Sarkar Jan 09 '21 at 23:34
-
0
Using Files.Copy works fine and I tested this code when renameTo() or FileInput/OutputStream code didn't work.
Try without the StandardCopyOption and the permissions of the destination folder are automatically inherited like this:
Files.copy(source.toPath(), destination.toPath());
Hope it can help.

Bibs0u67
- 106
- 5