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 OpenOptionOpenOption
Java provides two sets of OpenOptionjava.nio.file
- StandardOpenOption, this class has set of standard open options.
- LinkOpenOption, this class has the options as to how symbolic links are handled.
StandardOpenOption.READ
- This option is used to open the file for read access.
- Below is an example code that opens file for read access.
Note: READ is a default option for all read methods, so its not necessary to pass it explicitly.
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
- This option tells the system to create a new file and if the file already exists, then fail.
- Below is an example code, It always tries to create a new file and if the file already exists, then it throws
.FileAlreadyExistsException
public void createNew(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.CREATE_NEW);
}
StandardOpenOption.CREATE
- This option tells the system to create a new file only if it doesn't exist. If you pass both CREATE and CREATE_NEW as OpenOptions parameters, then CREATE_NEW option takes precedence.
- Below is an example code to create a new file if it doesn't exist.
public void createNew(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.CREATE);
}
StandardOpenOption.WRITE
- This option is used to open the file for write access.
- Below is an example code that opens file for write access (WRITE is a default option for all write methods, so its not necessary to pass it explicitly).
public void createNew(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.WRITE);
}
StandardOpenOption.APPEND
- This option is used to tell system to append the content.
- Below is an example code that appends text content.
public void append(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.APPEND);
}
StandardOpenOption.TRUNCATE_EXISTING
- This option tell thats if the file already exists and it is opened for WRITE access, then its length is truncated to 0.
- This option is ignored if the file is opened only for READ access.
- Below is an example code that truncates existing content (i.e. length is set to zero, meaning that discarding all of its content) and writes new content.
public void append(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.TRUNCATE_EXISTING);
}
StandardOpenOption.SPARSE
- This option tells the system that the file is a
sparse file , when used with CREATE_NEW option, this provides a hint that the new file will be a sparse file. - This option is ignored if the file system does not support sparse files.
- Below is an example code that truncates existing content (i.e. length is set to zero, in otherworks makes the file empty) and writes new content.
public void append(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.TRUNCATE_EXISTING);
}
StandardOpenOption.SYNC
- This option tells the system that all the data (file data and meta data managed by file system) will be written synchronously.
public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC);
}
StandardOpenOption.DSYNC
- This option tells the system that the file data will be written synchronously.
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
- This option tells the system to delete the file.
- It makes the best effort to delete the file when appropriate
method is called, if theclose() method is not called then a best effort attempt is made to try and delete the file when JVM terminates.close()
public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC);
}
StandardOpenOption.NOFOLLOW_LINKS
- This option tells the system not to follow symbolic links.
- By default, symbolic links are followed and the file attribute of the final target of the link is read. If the option NOFOLLOW_LINKS is present then symbolic links are not followed.
public boolean checkDirWithNoFollowLinks(String dir) {
return Files.isDirectory(Path.of(dir), LinkOption.NOFOLLOW_LINKS);
}
Above examples source code can be found at