Friday, September 21, 2018

Java 11 Features - java.nio.file.Files class new Methods | Read/write a string from to a file

In this blog post, We are going to learn the new methods added to java.nio.file.Files class.
Java 11 Features -   java.nio.file.Files class new Methods

java.nio.file.Files class 

This class is used for doing operations with files and directories of a file system. It provides various static methods for doing it.
with Java 11, Files class added below methods for converting file content to/from strings. 

Java 11 Files 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 this methods, We can achieve the following things
  • How to Read string from a file
  • How to Write a String into a file
These are commonly used operations when dealing with the file system.

Prior Java 11, Read String content from a File 


This example explains How to read the content of a file First read all bytes from a file using a readAllBytes method which returns Byte Array Convert this to String with 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 is

This is test content
As we have seen how we can read from the file and convert bytes to String method. Same can be done by readString() method

readString() method 

This method read all the content of a file and return as a string. This also contains an overloaded method for Charset for decoding bytes to characters
This method throws Exception in below cases

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

Syntax

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

example 

This example shows how to
  • read file content into a string
  • read file content into 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 is

Test content from a file
Test content from a file

writeString() method 

This method is used to write a string to File on filesystem
  • This method throws Exception in below cases java.lang.IllegalArgumentException - This will be thrown when invalid options are provided 
  • java.io.IOException - This exception throws when during IO operations error of writing a string to file 
  • java.lang.UnsupportedOperationException - This exception throws when unsupported operations are provided 
  • java.lang.SecurityException - This exception throws when there are permissions access issues for accessing files 

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 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 is
new string adding content in to a file

Related article


EmoticonEmoticon