When you open or create a file with Java's NIO API, you control its behaviour by passing one or more OpenOption values. An OpenOption answers questions like: Should I truncate the file or append to it? Should I fail if the file already exists? Should I open it for reading, writing, or both? Without an explicit option the API falls back to a per-method default, which can be surprising — knowing the defaults is just as important as knowing the options.

Java provides two sets of OpenOption implementations, both found in the package:

.READ public String read(String fileFullyQualifiedPath) throws IOException { InputStreamReader inputStreamReader = new InputStreamReader(Files.newInputStream(Path.of(fileFullyQualifiedPath), StandardOpenOption.READ)); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder sb = new StringBuilder(); String line; while((line = bufferedReader.readLine()) != null) { sb.append(line); } return sb.toString(); }
.CREATE_NEW public void createNew(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.CREATE_NEW); }
.CREATE public void createNew(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.CREATE); }
.WRITE public void createNew(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.WRITE); }
.APPEND public void append(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.APPEND); }
.TRUNCATE_EXISTING public void append(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.TRUNCATE_EXISTING); }
.SPARSE public void append(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.TRUNCATE_EXISTING); }
.SYNC public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC); }
.DSYNC public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC); }
.DELETE_ON_CLOSE public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException { Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC); }
.NOFOLLOW_LINKS public boolean checkDirWithNoFollowLinks(String dir) { return Files.isDirectory(Path.of(dir), LinkOption.NOFOLLOW_LINKS); }

Above examples source code can be found at GitHub link for Java code and JUnit tests can be found at GitHub link for Unit tests code