That may be so but if you look at the underlying code:
Code: Select all
BufferedWriter br = Files.newBufferedWriter(writeFile, Charset.forName("UTF-8"), new OpenOption[] {StandardOpenOption.APPEND, StandardOpenOption.CREATE_NEW});
If you go into method
newBufferedWriter, then into method
newOutputStream from the below snippet:
Code: Select all
Writer writer = new OutputStreamWriter(newOutputStream(path, options), encoder);
then
newOutputStream again from the below snippet:
Code: Select all
return provider(path).newOutputStream(path, options);
you'll see the below code is used every time you create a BufferedWriter using java.nio.Files:
Code: Select all
public OutputStream newOutputStream(Path path, OpenOption... options)
throws IOException
{
int len = options.length;
Set<OpenOption> opts = new HashSet<OpenOption>(len + 3);
if (len == 0) {
opts.add(StandardOpenOption.CREATE);
opts.add(StandardOpenOption.TRUNCATE_EXISTING);
} else {
for (OpenOption opt: options) {
if (opt == StandardOpenOption.READ)
throw new IllegalArgumentException("READ not allowed");
opts.add(opt);
}
}
opts.add(StandardOpenOption.WRITE);
return Channels.newOutputStream(newByteChannel(path, opts));
}
where the default options are CREATE and TRUNCATE_EXISTING, which are not added if at least one option is added by the user, and WRITE is always added as an option regardless. Plus you can see that it fails if it detects READ is being used.