Java 11 - New Methods in Path and Files

In Java 11, some methods are added java.nio.file.Path and java.nio.file.Files.

Changes in java.nio.file.Path

Java 11 added two new overloaded static methods in java.nio.file.Path to conveniently create a Path instance.

  • Path of(String, String[]): Returns a Path by converting a path string, or a sequence of strings that when joined form a path string. The Path is obtained by invoking the getPath method of the default FileSystem.
  • Path of(net.URI): Returns a Path by converting a URI.
PathOf.java
package com.dariawan.file;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;

public class PathOf {

    public static void main(String[] args) throws IOException {
        String tempFolder = System.getProperty("java.io.tmpdir");
        
        // Create Path from a sequence of Strings
        Path path1 = Path.of(tempFolder, "test.txt");
        System.out.println(path1);        
        System.out.println(Files.exists(path1));
        
        File file = new File(path1.toString());
        //Create the file
        if (file.createNewFile()) {
            System.out.println("File is created!");
        } else {
            System.out.println("File already exists.");
        }
        
        String uriFullPath = "file:///" + tempFolder.replace("\\", "/") + "test.txt";
        URI uri = URI.create(uriFullPath);
        System.out.println(uri);
      
        // Create Path from a URI
        Path path2 = Path.of(uri);
        System.out.println(path2);
        System.out.println(Files.exists(path2));
    }
}
                    

If we run the program for the first time:

C:\Users\Dariawan\AppData\Local\Temp\test.txt false File is created! file:///C:/Users/Dariawan/AppData/Local/Temp/test.txt C:\Users\Dariawan\AppData\Local\Temp\test.txt true

Changes in java.nio.file.Files

To read from a large file, I normally using BufferedReader combined with FileReader. And to write to "large" file, I will use BufferedWriter combined with FileWriter. But, let's think about a simple case where actually the file is simply can be handle as a simple String, the "combo" I mentioned before is very inconvenient.

Here’s where Java 11 interjects by adding readString and writeString to Files. Java 11 added following new methods in java.nio.file.Files class to directly read String from files and to directly write string to files:

ReadAndWriteString.java
package com.dariawan.file;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

public class ReadAndWriteString {

    static Path createTempPath() throws IOException {
        Path tempPath = Files.createTempFile("test", ".txt");
        File tempFile = tempPath.toFile();
        tempFile.deleteOnExit();
        
        return tempPath;
    }
    
    public static void main(String[] args) throws IOException {
        String testString = "The price is £100 or about €120";
        
        Path path1 = Files.writeString(createTempPath(), testString);
        System.out.println(path1);
        System.out.println(Files.readString(path1));        
        
        Charset charset = Charset.forName("ISO-8859-3");
        Path path2 = Files.writeString(createTempPath(), testString, charset);
        System.out.println(path2);
        String s = Files.readString(path2, charset);
        System.out.println(s);
    }
}
                    

With output:

C:\Users\Desson\AppData\Local\Temp\test3699164529979428936.txt The price is £100 or about €120 C:\Users\Desson\AppData\Local\Temp\test1260628730292518638.txt The price is £100 or about ?120

With the ability to read a file as single String, I create following program to feed my curiosity on how this new method will handle a multi-line file:

ReadMultilineString.java
package com.dariawan.file;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ReadMultilineString {

    public static void main(String[] args) throws IOException, URISyntaxException {
        var classLoader = ClassLoader.getSystemClassLoader();
        var url = classLoader.getResource("test-read.txt");
        
        Path path = Path.of(url.toURI());
        System.out.println(path);
        System.out.println(Files.exists(path));
        
        System.out.println(Files.readString(path));
    }
}
                    

with test-read.txt has following:

Every program has (at least) two purposes: 1. the one for which it was written 2. and another for which it wasn't.

And will produce following output:

D:\Projects\dariawan11\target\classes\test-read.txt true Every program has (at least) two purposes: 1. the one for which it was written 2. and another for which it wasn't.