Java 11 Features - java.nio.file.Files class new Methods

In this blog post, we will explore the new methods added to the java.nio.file.Files class in Java 11.

What is java.nio.file.Files class in Java 11?

This class is used for performing operations with files and directories in a file system. It provides various static methods for these operations.

With Java 11, the Files class has added the following methods for converting file content to/from strings.

It introduced the following Java 11 Files and new methods:

  • String Files.readString(Path path)
  • String Files.readString(Path, Charset)
  • Path Files.write(Path, CharSequence, OpenOption…)
  • Path Files.write(Path, CharSequence, Charset, OpenOption…)

With these methods, We can achieve the following things.

  • How to Read a string from a file
  • How to Write a String into a file

These are commonly used operations when dealing with the file system.

How to Read String content from a File before Java 11?

This example explains how to read the content of a file. First, read all bytes from a file using the readAllBytes method, which returns a Byte Array. Next, convert this to a String with a byte array as a parameter for a string constructor.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Files1 {
    public static void main(String[] args) {
        // First Create a Path Object
        Path path = Paths.get("b://files1.txt");
        //Create a file
        try {
            byte[] bs = Files.readAllBytes(path);
            System.out.println(new String(bs));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

This is test content

As seen above, we read from the file and convert bytes to the String method. Alternatively, the same can be done using the readString() method.

Read from a file using readString() method

This method reads all the content of a file and returns it as a string. It also contains an overloaded method for Charset for decoding bytes to characters.

This method throws an exception in the following cases

It also contains an overloaded method for Charset for decoding bytes to characters.

This method throws an Exception in the below cases.

  • java.nio.file.NoSuchFileException: if the file does not exist on a file system
  • java.io.IOException: this exception is thrown during IO operations of transferring the file to the String
  • java.lang.OutOfMemoryError: this exception is thrown when the file size is large, i.e., a 2GB file size
  • java.lang.SecurityException: this exception is thrown when there are permissions access issues for accessing files

Here is a Syntax

 public static String readString(Path path) throws IOException
 public static String readString(Path path, Charset cs) throws IOException

Here is an example that explains

  • Read file content into a string
  • Read file content into a String using UTF-8 Decoding
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Files1 {
    public static void main(String[] args) {
        // First Create a Path Object
        Path path = Paths.get("b://files1.txt");
        //Read a file
        try {
            String string = Files.readString(path);
            System.out.println(string);
            String string1 = Files.readString(path,StandardCharsets.UTF_8);
            System.out.println(string1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Test content from a file
Test content from a file

How to write a string to a file using the writeString() method?

This method is used to write a string to a file on the filesystem. It throws an exception in the following cases.

  • java.lang.IllegalArgumentException: this will be thrown when invalid options are provided
  • java.io.IOException: this exception is thrown during IO operations error of writing a string to a file
  • java.lang.UnsupportedOperationException: this exception is thrown when unsupported operations are provided
  • java.lang.SecurityException: this exception is thrown when there are permissions access issues for accessing files

Following is a Signature

public static Path writeString(Path path, CharSequence csq, OpenOption... options) throws IOException
public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options)

Example:

This example shows how to write a string into a file, write a string into a file using UTF-8 Decoding.

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Files1 {
    public static void main(String[] args) {
        // First Create a Path Object
        Path path = Paths.get("b://files1.txt");
        CharSequence sequenceString = "new string adding content in to a file";

        //Read a file
        try {
            Path newPath= Files.writeString(path,sequenceString);
            String string1 = Files.readString(newPath,StandardCharsets.UTF_8);
            System.out.println(string1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

new string adding content into a file

Conclusion

In conclusion, we have learned about the new Files methods with examples introduced in Java 11.