Skip to content
Related Articles

Related Articles

Files Class readString() Method in Java with Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 05 Feb, 2021

The readString() method of File Class in Java is used to read contents to the specified file.




path - File path with data type as Path

Return Value: This method returns the content of the file in String format.

Below are two overloaded forms of the readString() method.

public static String readString​(Path path) throws IOException 
public static String readString​(Path path, Charset cs) throws IOException
  • Using the UTF-8 charset in the first method reads all content from a file into a string, decoding from bytes to characters.
  • The second method does the same using the specified charset.

Input File:

Input File

Below is the implementation of the problem statement:


// Implementation fo readString() method in Java
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
    public static void main(String[] args)
        // creating path
        Path filePath
            = Paths.get("/home/mayur/", "temp", "gfg.txt");
            // reading file from given path
            String content = Files.readString(filePath);
            // printing the content
        catch (IOException e) {


Hello from GFG!
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!