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));
Answer from Franz Ebner on Stack Overflow Top answer 1 of 3
37
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));
2 of 3
7
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.
Java2s
java2s.com › Tutorials › Java › java.nio.file › Files › Java_Files_write_Path_path_byte_bytes_OpenOption_options_.htm
Java Tutorial - Java Files.write(Path path, byte[] bytes, OpenOption ... options)
// www . j a va 2s . c o m import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; public class Main { public static void main(String[] args) { Path ball_path = Paths.get("C:/tutorial/photos", "ball.png"); byte[] ball_bytes = new byte[]{(byte) 0x89, (byte) 0x50, (byte) 0x4e, }; try { Files.write(ball_path, ball_bytes); } catch (IOException e) { System.err.println(e); } } }
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › StandardOpenOption.html
StandardOpenOption (Java Platform SE 8 )
March 16, 2026 - Since: 1.7 · clone, compareTo, ... access. public static final StandardOpenOption APPEND · If the file is opened for WRITE access then bytes will be written to the end of the file rather than the beginning....
Rust
doc.rust-lang.org › std › fs › struct.OpenOptions.html
OpenOptions in std::fs - Rust
In order for the file to be created, OpenOptions::write or OpenOptions::append access must be used.
Java Tips
javatips.net › api › java.nio.file.openoption
Java Examples for java.nio.file.OpenOption - Javatips.net
The following java examples will help you to understand the usage of java.nio.file.OpenOption. These source code samples are taken from different open source projects. ... protected static boolean writeFile(Path file, List<String> lines, boolean append) { assert (file != null) && (lines != null) && (lines.size() > 0); OpenOption[] options; Charset charset = Charset.defaultCharset(); if (append) { options = new OpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE }; } else { options = new OpenOption[] { StandardOpenOption.WRITE, StandardOpenOption.CREATE
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › class-use › OpenOption.html
Uses of Interface java.nio.file.OpenOption (Java Platform SE 8 )
July 15, 2025 - Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Program Creek
programcreek.com › java-api-examples
java.nio.file.OpenOption Java Examples
You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar. Example #1 · static OpenOption[] getOpenOptions(boolean append) { OpenOption[] defaultOpenOptions = {StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE}; OpenOption[] appendOpenOptions = {StandardOpenOption.APPEND, StandardOpenOption.WRITE}; return append ?
Unal
ungrid.unal.edu.co › Java8 › tutorial › essential › io › file.html
Reading, Writing, and Creating Files (The Java™ Tutorials > Essential Classes > Basic I/O)
This method opens or creates a file for writing bytes and returns an unbuffered output stream. The method takes an optional OpenOption parameter. If no open options are specified, and the file does not exist, a new file is created. If the file exists, it is truncated.
Oracle
docs.oracle.com › javase › 7 › docs › api › › › java › nio › file › class-use › OpenOption.html
Uses of Interface java.nio.file.OpenOption (Java Platform SE 7 )
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Oracle
docs.oracle.com › javase › 7 › docs › api › java › nio › file › OpenOption.html
OpenOption (Java Platform SE 7 )
java.nio.file · All Known Implementing Classes: LinkOption, StandardOpenOption · public interface OpenOption · An object that configures how to open or create a file. Objects of this type are used by methods such as newOutputStream, newByteChannel, FileChannel.open, and AsynchronousFile...
Stack Overflow
stackoverflow.com › questions › 69341037 › openoptions-write-not-working-properly-when-writing-to-file
rust - OpenOptions write not working properly when writing to file - Stack Overflow
Maybe you would better use simple fs::*, as i know, OpenOptions if you will create multiprocess/thread code this function will face with races, so possible(as i understood: read all then delete all and write): use std::fs::{File, write}; use std::io::Result; fn main() -> Result<()>{ let mut file = File::open("foo.txt")?; //Read(create copy if you need something simultaneous read before) // file.try_clone()?;//simply, both files changes file.read_to_end(&mut your_string_or_vec)?; //..... Your code //and then truncate all file_copy.set_len(0)?; write("foo.txt", formatted.as_bytes())?; }
Oracle
docs.oracle.com › javase › 7 › docs › api › java › nio › file › StandardOpenOption.html
StandardOpenOption (Java Platform SE 7 )
Since: 1.7 · clone, compareTo, ... access. public static final StandardOpenOption APPEND · If the file is opened for WRITE access then bytes will be written to the end of the file rather than the beginning....
Mitaa
mitaa.github.io › rust › doc › std › fs › struct.OpenOptions.html
std::fs::OpenOptions - Rust
If the file already exists, any write calls on it will overwrite its contents, without truncating it. fn main() { use std::fs::OpenOptions; let file = OpenOptions::new().write(true).open("foo.txt"); }
W3cubDocs
docs.w3cub.com › rust › std › fs › struct.openoptions.html
std::fs::OpenOptions - Rust - W3cubDocs
In order for the file to be created, OpenOptions::write or OpenOptions::append access must be used.
Android Developers
developer.android.com › api reference › standardopenoption
StandardOpenOption | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Rust
docs.rs › async-std › latest › async_std › fs › struct.OpenOptions.html
OpenOptions in async_std::fs - Rust
When set to true, this option means the file will be writable after opening and the file cursor will be moved to the end of file before every write operation. use async_std::fs::OpenOptions; let file = OpenOptions::new() .append(true) .open("a.txt") .await?;
docs.rs
docs.rs › deterministic › latest › deterministic › file › struct.OpenOptions.html
OpenOptions in deterministic::file - Rust
[Other]: Filesystem-level errors: full disk, write permission requested on a read-only file system, exceeded disk quota, too many open files, too long filename, too many symbolic links in the specified path (Unix-like systems only), etc. use std::fs::OpenOptions; let file = OpenOptions::ne...