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

- 4,951
- 3
- 39
- 57

- 22,436
- 15
- 82
- 99
-
Common combinations are specified as defaults anyway. – Franz Ebner Jan 01 '14 at 19:14
-
@franzebner Are they? – Aleksandr Dubinsky Jan 01 '14 at 23:18
-
1As listed beyond, yes they are :) The OpenOptions in Files.new* are optional, and defaults are predefined. – Franz Ebner Jan 01 '14 at 23:29
3 Answers
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));

- 4,951
- 3
- 39
- 57
-
Those are good suggestions, thanks! Btw, do you know if it's possible to get these methods to create necessary directories? – Aleksandr Dubinsky Jan 02 '14 at 00:02
-
As a hint: Files.createDirectories(dir, attrs) and Files.createDirectory(dir, attrs)! Note the diff. – Franz Ebner Jan 02 '14 at 00:09
-
I mean that FileUtils.writeByteArrayToFile will create non-existent directories in the path, while Files.write fails. – Aleksandr Dubinsky Jan 02 '14 at 00:19
-
-
What's implementation specific? I know Commons isn't JDK. I'm asking if there is a way to get the same behavior. – Aleksandr Dubinsky Jan 02 '14 at 02:11
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44325/discussion-between-franz-ebner-and-aleksandr-dubinsky) – Franz Ebner Jan 02 '14 at 13:07
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
Can I pass an array as arguments to a method with variable arguments in Java?
So...
OpenOption myOptions[] = {StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING};
OutputStream foo=OutputStream.newOutputStream(myPath,myOptions);
Caveat: Untested.
-
This will definitely work. I was kind of hoping that Apache Commons or Guava have already done this. But it seems few people use NIO.2 in Java 7 – Aleksandr Dubinsky Dec 31 '13 at 09:06
-
You could vote up the answer, since it will work and nothing better's on tap. But since the bounty isn't awarded unless it gets at least two up votes, I'm not sure that would make a difference. – keshlam Jan 01 '14 at 03:39
java.nio.file.Files
has 5 flavours of methods with OpenOption
varargs parameters:
Files
.newBufferedWriter(...)
.write(...)
.newOutputStream(...)
.newInputStream(...)
.newByteChannel(...)
They directly don't restrict any OpenOption
combination, but all of them under the hood call to some of these 3 methods at 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.newOutputStream(...)
is called by:
Files
.newBufferedWriter(...)
.newOutputStream(...)
.write(...)
abstract FileSystemProvider.newByteChannel(...)
is called by:
Files.newByteChannel(...)
FileSystemProvider.newInputStream(...)
FileSystemProvider.newOutputStream(...)
OptenOption
combination restrictions:
- FileSystemProvider.newInputStream(...)
- UnsupportedOperationException: WRITE || APPEND
- FileSystemProvider.newOutputStream(...)
- Implicitly: WRITE
- IllegalArgumentException: READ
- default (if non options): CREATE && TRUNCATE_EXISTING
The abstract FileSystemProvider.newByteChannel(...)
method has a platform dependent implementation, which may extend the OpenOption
combination restrictions (as in sun.nio.fs.WindowsFileSystemProvider
).
All Files method which uses OpenOption
vargars under the hood ends in the abstract FileSystemProvider.newByteChannel(...)
, which implementation is platform dependent. So, the OpenOption
combinations restriction in Files methods are platform dependent.

- 822
- 1
- 8
- 18