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:
- StandardOpenOption, this class has set of standard open options.
- LinkOpenOption, this class has the options as to how symbolic links are handled.
.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();
}
.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 .
public void createNew(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath),text, StandardOpenOption.CREATE_NEW);
}
.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);
}
.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);
}
.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);
}
.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);
}
.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);
}
.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);
}
.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);
}
.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 the method is not called then a best effort attempt is made to try and delete the file when JVM terminates.
public void writeInSync(String fileFullyQualifiedPath, String text) throws IOException {
Files.writeString(Path.of(fileFullyQualifiedPath), text, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.SYNC);
}
.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
GitHub link for Java code
and JUnit tests can be found at
GitHub link for Unit tests code