Files Class readString() Method in Java with Examples
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.
Below is the implementation of the problem statement:
Hello from GFG!
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.