是否有一种简洁,惯用的方式(也许使用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));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句