Java : StandardCopyOption (File) con ejemplos
StandardCopyOption (Java SE 22 & JDK 22) en Java con ejemplos.
Encontrará muestras de código para la mayoría de los métodos StandardCopyOption.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
Annotation :
- Most of the code examples in this page are run on Windows.
final var source = Path.of("R:", "java-work", "src.txt");
System.out.println(source); // R:\java-work\src.txt
final var target = Path.of("R:", "java-work", "dst.txt");
System.out.println(target); // R:\java-work\dst.txt
Files.writeString(source, "abc");
Files.writeString(target, "XYZ");
System.out.println(Files.readString(source)); // abc
System.out.println(Files.readString(target)); // XYZ
try {
System.out.println("-- copy --");
Files.copy(source, target);
} catch (FileAlreadyExistsException e) {
System.out.println("FileAlreadyExistsException! : " + e.getMessage());
}
// Result
// ↓
//-- copy --
//FileAlreadyExistsException! : R:\java-work\dst.txt
System.out.println(Files.readString(source)); // abc
System.out.println(Files.readString(target)); // XYZ
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
System.out.println(Files.readString(source)); // abc
System.out.println(Files.readString(target)); // abc
Enum Constants
ATOMIC_MOVE
final var source = Path.of("D:", "java-work", "src.txt");
System.out.println(source); // D:\java-work\src.txt
final var target = Path.of("R:", "java-work", "dst.txt");
System.out.println(target); // R:\java-work\dst.txt
Files.writeString(source, "abc");
System.out.println(Files.readString(source)); // abc
System.out.println(Files.exists(target)); // false
try {
System.out.println("-- move --");
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException e) {
System.out.println("AtomicMoveNotSupportedException!");
}
// Result
// ↓
//-- move --
//AtomicMoveNotSupportedException!
System.out.println(Files.readString(source)); // abc
System.out.println(Files.exists(target)); // false
Files.move(source, target);
System.out.println(Files.exists(source)); // false
System.out.println(Files.readString(target)); // abc
COPY_ATTRIBUTES
// The following code runs on Linux.
System.out.println(System.getProperty("os.name")); // Linux
final var source = Path.of("src.txt");
System.out.println(source); // src.txt
Files.createFile(source);
Files.setLastModifiedTime(source, FileTime.fromMillis(0));
System.out.println(Files.getLastModifiedTime(source)); // 1970-01-01T00:00:00Z
final var target1 = Path.of("dst1.txt");
System.out.println(target1); // dst1.txt
final var target2 = Path.of("dst2.txt");
System.out.println(target2); // dst2.txt
Files.copy(source, target1);
Files.copy(source, target2, StandardCopyOption.COPY_ATTRIBUTES);
System.out.println(Files.getLastModifiedTime(target1)); // 2024-11-06T06:28:38.017333603Z
System.out.println(Files.getLastModifiedTime(target2)); // 1970-01-01T00:00:00Z
REPLACE_EXISTING
final var source = Path.of("R:", "java-work", "src.txt");
System.out.println(source); // R:\java-work\src.txt
final var target = Path.of("R:", "java-work", "dst.txt");
System.out.println(target); // R:\java-work\dst.txt
Files.writeString(source, "abc");
Files.writeString(target, "XYZ");
System.out.println(Files.readString(source)); // abc
System.out.println(Files.readString(target)); // XYZ
try {
System.out.println("-- copy --");
Files.copy(source, target);
} catch (FileAlreadyExistsException e) {
System.out.println("FileAlreadyExistsException! : " + e.getMessage());
}
// Result
// ↓
//-- copy --
//FileAlreadyExistsException! : R:\java-work\dst.txt
System.out.println(Files.readString(source)); // abc
System.out.println(Files.readString(target)); // XYZ
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
System.out.println(Files.readString(source)); // abc
System.out.println(Files.readString(target)); // abc
Methods
static StandardCopyOption valueOf (String name)
final var ret1 = StandardCopyOption.valueOf("REPLACE_EXISTING");
System.out.println(ret1); // REPLACE_EXISTING
final var ret2 = StandardCopyOption.valueOf("COPY_ATTRIBUTES");
System.out.println(ret2); // COPY_ATTRIBUTES
static StandardCopyOption[] values ()
for (final var value : StandardCopyOption.values()) {
System.out.println(value);
}
// Result
// ↓
//REPLACE_EXISTING
//COPY_ATTRIBUTES
//ATOMIC_MOVE
Methods declared in Enum
clone, compareTo, describeConstable, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Consulte el siguiente enlace.