Java : StandardCopyOption (File) with Examples

StandardCopyOption (Java SE 19 & JDK 19) API Examples.
You will find code examples on most StandardCopyOption methods.


Summary

Defines the standard copy options.

Class diagram

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

Annotation :

  • Most examples run on Windows 10.

Enum Constants

ATOMIC_MOVE

Move the file as an atomic file system operation.

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

Copy attributes to the new file.

// The following code runs on Linux.

final var source = Path.of("src.txt");
System.out.println(source); // src.txt

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.createFile(source);
Files.setLastModifiedTime(source, FileTime.fromMillis(0));

Files.copy(source, target1);
Files.copy(source, target2, StandardCopyOption.COPY_ATTRIBUTES);

System.out.println(Files.getLastModifiedTime(source)); // 1970-01-01T00:00:00Z
System.out.println(Files.getLastModifiedTime(target1)); // 2022-12-08T09:24:58.58665956Z
System.out.println(Files.getLastModifiedTime(target2)); // 1970-01-01T00:00:00Z

REPLACE_EXISTING

Replace an existing file if it exists.

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)

Returns the enum constant of this class with the specified 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 ()

Returns an array containing the constants of this enum class, in the order they are declared.

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

Please see the link below.


Related posts

To top of page