HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 11 / Java 11 – Files readString() API

Java 11 – Files readString() API

Learn to read a file to string in Java using Files.readString(path) method. This API has been introduced in Java 11.

1. Files readString() methods

java.nio.file.Files class has two overloaded methods.

public static String readString​(Path path) throws IOException

public static String readString​(Path path, Charset cs) throws IOException
  • First method reads all content from a file into a string, decoding from bytes to characters using the UTF-8 charset.

    The method ensures that the file is closed when all content have been read or an I/O error, or other runtime exception, is thrown.

  • First method is equivalent to readString(path, StandardCharsets.UTF_8).
  • Second method does the same with with only using the specified charset.
  • Please note that these methods are not intended for reading very large files. Otherwise they may throw OutOfMemoryError if the file is extremely large, e.g. larger than 2GB.

2. Files readString() example

Java program to read a file into string using Files.readString() method.

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

public class Main 
{
	public static void main(String[] args) 
	{
		Path filePath = Paths.get("C:/", "temp", "test.txt");

		try 
		{
			String content = Files.readString(filePath);

			System.out.println(content);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}

Program output.

Hello Java Learner !!

Where the file c:/temp/test.txt is this.

Hello Java Learner !!

Drop me your questions in comments section.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Rowan

    September 8, 2019

    Hey, I’m using readString on an xml file, but when I got to use a string method like .getLength() it just ignores it. I print to the console and it shows the string but the print of the length shows nothing

Comments are closed on this article!

Search Tutorials

Java 11 Tutorial

  • Java 11 – New features
  • Java 11 – String.isBlank()
  • Java 11 – String.lines()
  • Java 11 – String.repeat()
  • Java 11 – String.strip()
  • Java 11 – Files.readString()
  • Java 11 – Files.writeString()

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)