1 year ago

#50931

test-img

MarioSemo

java Files.copy copy readonly attribute without COPY_ATTRIBUTE option

i try to copy files without their filesystem attributes. there are some readonly only files, but i do not want their copy to be readonly. so i use

Files.copy(src,target,REPLACE_EXISTING);

but... the target gets the readonly attribute without COPY_ATTRIBUTES too. (OS: Win10, Envir: JavaSE17)

Samplecode:

final CopyOption[] c1 = new CopyOption[] {};
final CopyOption[] c2 = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING };
final CopyOption[] c3 = new CopyOption[] { StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES };
    
Files.copy(Paths.get(sourceFileName), Paths.get(targetFileName + "_c1"), c1);
Files.copy(Paths.get(sourceFileName), Paths.get(targetFileName + "_c2"), c2);
Files.copy(Paths.get(sourceFileName), Paths.get(targetFileName + "_c3"), c3);

Result:

f:\TMP\15049011634773449363\LIB__fuelsys_dd_controllerLib__0.0.1\INCLUDES>attrib *
    
AR F:\TMP\1....\LIB__fuelsys_dd_controllerLib__0.0.1\INCLUDES\fuelsys_dd_controller.c_c1
AR F:\TMP\1....\LIB__fuelsys_dd_controllerLib__0.0.1\INCLUDES\fuelsys_dd_controller.c_c2
AR F:\TMP\1....\LIB__fuelsys_dd_controllerLib__0.0.1\INCLUDES\fuelsys_dd_controller.c_c3

I can fix the behavior with

new File(targetFileName).setWritable(true);

but its a bit annoying to have to reset the attribute all the time. I dont want to copy it.

java

java.nio.file

readonly-attribute

0 Answers

Your Answer

Accepted video resources