🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
In this guide, you will learn about the Files writeString() method in Java programming and how to use it with an example.
1. Files writeString() Method Overview
Definition:
The writeString() method, introduced in Java 11, is a part of the Files class. It's used to write a CharSequence to a file. This method provides a succinct way to write content to a file, reducing the need for boilerplate code.
Syntax:
1. Files.writeString(Path path, CharSequence cs, OpenOption... options) throws IOException
2. Files.writeString(Path path, CharSequence cs, Charset cs, OpenOption... options) throws IOException
Parameters:
- path: The path to the file to write. It's a Path object indicating the file location.
- cs: The character sequence to write to the file.
- options: Optional. Specifies how the file is created or opened (e.g., CREATE, TRUNCATE_EXISTING, WRITE).
- cs: The charset to use for encoding.
Key Points:
- If the file already exists, the method overwrites it by default.
- The method opens or creates the file, writes the CharSequence in it, and then closes the file.
- Throws IOException if an I/O error occurs.
- Various OpenOption values can be specified, like StandardOpenOption.WRITE, StandardOpenOption.CREATE, etc.
- If no options are specified, this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present.
2. Files writeString() Method Example
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.io.IOException;
public class WriteStringExample {
public static void main(String[] args) {
Path filePath = Paths.get("example.txt");
String content = "Java Files.writeString() Example";
try {
Files.writeString(filePath, content, StandardOpenOption.CREATE);
System.out.println("Content written to the file successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Content written to the file successfully!
Explanation:
In the given example, we specify a file path and the content to write to the file. We then use the Files.writeString() method with StandardOpenOption.CREATE to either create a new file or overwrite an existing one. After successfully writing the content to the file, a confirmation message is printed to the console.
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