File Open Options in Java

Java NIO file methods accept StandardOpenOption values that control how a file is opened or created: whether to append, truncate, create if absent, or open read-only. This article covers all StandardOpenOption constants with examples showing how to combine them in Files.write(), Files.newOutputStream(), and FileChannel.open().

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 java.nio.file package:

StandardOpenOption.READ

Java
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();
}

StandardOpenOption.CREATE_NEW

Java
public void createNew(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.CREATE_NEW);
}

StandardOpenOption.CREATE

Java
public void createNew(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.CREATE);
}

StandardOpenOption.WRITE

Java
public void createNew(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.WRITE);
}

StandardOpenOption.APPEND

Java
public void append(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.APPEND);
}

StandardOpenOption.TRUNCATE_EXISTING

Java
public void append(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.TRUNCATE_EXISTING);
}

StandardOpenOption.SPARSE

Java
public void append(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.TRUNCATE_EXISTING);
}

StandardOpenOption.SYNC

Java
public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC);
}

StandardOpenOption.DSYNC

Java
public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC);
}

StandardOpenOption.DELETE_ON_CLOSE

Java
public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException {
  Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC);
}

StandardOpenOption.NOFOLLOW_LINKS

Java
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