The Wayback Machine - https://web.archive.org/web/20240716023506/https://www.geeksforgeeks.org/files-class-readstring-method-in-java-with-examples/
Open In App

Files Class readString() Method in Java with Examples

Last Updated : 27 Jan, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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

Syntax:

Files.readString(filePath)

Parameters: 

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:

Java




// Implementation for readString() method in Java
import java.io.IOException;
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");
        try
 
        {
            // reading file from given path
            String content = Files.readString(filePath);
            // printing the content
            System.out.println(content);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Output:

Hello from GFG!


Similar Reads

Files Class writeString() Method in Java with Examples
The writeString() method of File Class in Java is used to write contents to the specified file. Syntax: Files.writeString(path, string, options) Parameters: path - File path with data type as Pathstring - a specified string which will enter in the file with return type String.options - Different options to enter the string in the file. Like append
2 min read
mismatch() Method of java.nio.file.Files Utility Class with Examples
mismatch() method is newly added in java jdk-12 in the java.nio.file.Files class. This method is mainly used to compare each byte of data of two files from start and return the point at which the bit differs. If the bit doesn't differ at any point then it returns "-1L" indicating that files are the same and exact copies of each other. This method m
2 min read
Files deleteIfExists() method in Java with Examples
The deleteIfExists() method of java.nio.file.Files help us to delete a file if the file exists at the path. we pass the path of the file as a parameter to this method. This method will return true if the file was deleted by this method; false if the file could not be deleted because it did not exist. If the file is a symbolic link then the symbolic
3 min read
Files delete() method in Java with Examples
The delete() method of java.nio.file.Files help us to delete a file located at the path passed as a parameter. This method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted. If the file is a directory then this method will delete
3 min read
Files getFileStore() method in Java with Examples
getFileStore() method of java.nio.file.Files help us to return the FileStore object which represents the file store where a file is located. Once you get a reference to the FileStore you can apply filestore type of operation to get information of filestore. Syntax: public static FileStore getFileStore(Path path) throws IOException Parameters: This
2 min read
Files isHidden() method in Java with Examples
isHidden() method of java.nio.file.Files help us to check whether or not a file is hidden. This method return true if the file is considered hidden else it returns false. The exact definition of hidden is platform or provider dependent. On UNIX, for example, a file is considered to be hidden if its name begins with a period character ('.'). On Wind
2 min read
Files isReadable() method in Java with Examples
isReadable() method of java.nio.file.Files help us to check whether Java virtual machine has appropriate privileges that would allow it to open this file for reading or not. This method test file is readable or not. This method checks that a file exists or not and if file exists then it is readable or not. This method returns true if the file exist
2 min read
Files isWritable() method in Java with Examples
isWritable() method of java.nio.file.Files help us to check whether Java virtual machine has appropriate privileges that would allow it to open this file for writing or not. It means this method test file is writable or not. This method checks that a file exists or not and if file exists then it is writable or not. This method returns true if the f
2 min read
Files isExecutable() method in Java with Examples
isExecutable() method of java.nio.file.Files help us to check whether a file is executable. This method checks that a file exists on this path or not and if it exists then Java virtual machine has appropriate privileges to execute the file or not. The semantics may differ when checking access to a directory. For example, on UNIX systems, checking f
2 min read
Files size() method in Java with Examples
size() method of java.nio.file.Files help us to get the size of a file (in bytes). This method returns the file size, in bytes by taking the path of the file as a parameter. The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons. The size of files that are not regular files is impl
2 min read
Practice Tags :
three90RightbarBannerImg