Java : StandardCopyOption (File) 示例

Java 中的 StandardCopyOption (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 StandardCopyOption 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

定义标准复印选项。 (机器翻译)

Class diagram

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

请参阅下面的链接。


相关文章

To top of page