🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
In this guide, you will learn about the Files readString() method in Java programming and how to use it with an example.
1. Files readString() Method Overview
Definition:
The readString() method belongs to the Files class in Java and is introduced in Java 11. This method is used to read all content from a file into a string, making it convenient to read file content with a single line of code.
Syntax:
1. Files.readString(Path path) throws IOException
2. Files.readString(Path path, Charset cs) throws IOException
Parameters:
- path: The Path object that locates the file to read.
- cs: Optional. The Charset to be used to decode the bytes to characters. If not provided, the UTF-8 charset is used by default.
Key Points:
- The method reads all the content of the file and returns it as a single String.
- The Charset for decoding can be optionally specified, default is UTF-8.
- Throws IOException if an I/O error occurs while reading from the file.
- Useful for reading small files, but care should be taken with large files to avoid OutOfMemoryError.
2. Files readString() Method Example
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class ReadStringExample {
public static void main(String[] args) {
Path filePath = Paths.get("example.txt");
try {
String content = Files.readString(filePath);
System.out.println("Content read from the file:");
System.out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Content read from the file: Java Files.writeString() Example
Explanation:
In this example, we specify a path to the file we want to read. We use the Files.readString() method to read the content of the file into a string. Then, we print the content to the console. If the file is not found or an I/O error occurs, the method will throw an IOException.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment