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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20663841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:25:39  来源:igfitidea点击:

Quickest way to use common OpenOption combinations

javanionio2

提问by Aleksandr Dubinsky

Is there a concise, idiomatic way (maybe using Apache Commons) to specify common combinations of OpenOption like StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING

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

采纳答案by Franz Ebner

These are the easy possibilities you have.

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

Static Imports, to increase readability:

静态导入,以提高可读性:

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

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

Use defaults:

使用默认值:

     //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)

Finally it's possible to specify optionsets like this:

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

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

回答by keshlam

The best suggestion I can offer would be to cheat on the equivalence of T... and T[], which one of the other stackoverflow discussions says should work

我能提供的最好的建议是在 T... 和 T[] 的等价性上作弊,其他 stackoverflow 讨论中的一个说应该有效

Can I pass an array as arguments to a method with variable arguments in Java?

我可以将数组作为参数传递给 Java 中带有可变参数的方法吗?

So...

所以...

OpenOption myOptions[] = {StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING};
OutputStream foo=OutputStream.newOutputStream(myPath,myOptions);

Caveat: Untested.

警告:未经测试。

回答by zeugor

java.nio.file.Fileshas 5 flavours of methods with OpenOptionvarargs parameters:

java.nio.file.Files有 5 种带有OpenOption可变参数的方法:

Files
    .newBufferedWriter(...)
    .write(...)
    .newOutputStream(...)
    .newInputStream(...) 
    .newByteChannel(...)

They directly don't restrict any OpenOptioncombination, but all of them under the hood call to some of these 3 methods at java.nio.file.spi.FileSystemProvider:

它们直接不限制任何OpenOption组合,但所有这些都在幕后调用了以下 3 种方法中的一些java.nio.file.spi.FileSystemProvider

FileSystemProvider
    .newInputStream(Path, OpenOption...)
    .newOutputStream(Path, OpenOption...)
    .newByteChannel(Path, Set<? extends OpenOption>, FileAttribute<?>...)

FileSystemProvider.newInputStream(...)is called by: Files.newInputStream(...)

FileSystemProvider.newInputStream(...)被称为: Files.newInputStream(...)

FileSystemProvider.newOutputStream(...)is called by:

FileSystemProvider.newOutputStream(...)被称为:

Files
    .newBufferedWriter(...)
    .newOutputStream(...)
    .write(...)

abstract FileSystemProvider.newByteChannel(...)is called by:

抽象FileSystemProvider.newByteChannel(...)被称为:

  • Files.newByteChannel(...)
  • FileSystemProvider.newInputStream(...)
  • FileSystemProvider.newOutputStream(...)
  • Files.newByteChannel(...)
  • FileSystemProvider.newInputStream(...)
  • FileSystemProvider.newOutputStream(...)

OptenOptioncombination restrictions:

OptenOption组合限制:

  • FileSystemProvider.newInputStream(...)
    • UnsupportedOperationException: WRITE || APPEND
  • FileSystemProvider.newOutputStream(...)
    • Implicitly: WRITE
    • IllegalArgumentException: READ
    • default (if non options): CREATE && TRUNCATE_EXISTING
  • FileSystemProvider.newInputStream(...)
    • UnsupportedOperationException: WRITE || 附加
  • FileSystemProvider.newOutputStream(...)
    • 隐式:写
    • IllegalArgumentException:读取
    • 默认(如果没有选项):CREATE && TRUNCATE_EXISTING

The abstractFileSystemProvider.newByteChannel(...)method has a platform dependent implementation, which may extend the OpenOptioncombination restrictions (as in sun.nio.fs.WindowsFileSystemProvider).

抽象FileSystemProvider.newByteChannel(...)方法具有依赖于平台的实现中,其可以延长OpenOption组合限制(如在sun.nio.fs.WindowsFileSystemProvider)。

All Files method which uses OpenOptionvargars under the hood ends in the abstract FileSystemProvider.newByteChannel(...), which implementation is platform dependent. So, the OpenOptioncombinations restriction in Files methods are platform dependent.

OpenOption在引擎盖下使用vargars 的所有 Files 方法都以abstract 结尾FileSystemProvider.newByteChannel(...),该实现取决于平台。因此,OpenOptionFiles 方法中的组合限制取决于平台。