gpt4 book ai didi

java - 使用常见 OpenOption 组合的最快方法

转载 作者:太空狗 更新时间:2023-10-29 22:42:51 29 4
gpt4 key购买 nike

是否有一种简洁、惯用的方式(可能使用 Apache Commons)来指定 OpenOption 的常见组合,如 StandardOpenOption.WRITE、StandardOpenOption.CREATE、StandardOpenOption.TRUNCATE_EXISTING

最佳答案

这些是您拥有的简单可能性。

静态导入,增加可读性:

import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static java.nio.file.StandardOpenOption.WRITE;

OpenOption[] options = new OpenOption[] { WRITE, CREATE_NEW };

使用默认值:

     //no Options anyway
Files.newBufferedReader(path, cs)

//default: CREATE, TRUNCATE_EXISTING, and WRITE not allowed: READ
Files.newBufferedWriter(path, cs, options)

//default: READ not allowed: WRITE
Files.newInputStream(path, options)

//default: CREATE, TRUNCATE_EXISTING, and WRITE not allowed: READ
Files.newOutputStream(path, options)

//default: READ do whatever you want
Files.newByteChannel(path, options)

最后可以像这样指定选项集:

     Files.newByteChannel(path, EnumSet.of(CREATE_NEW, WRITE));

关于java - 使用常见 OpenOption 组合的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20663841/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com