🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The Readable interface in Java is a functional interface that represents a source of characters. It allows reading a sequence of characters into a CharBuffer.
Table of Contents
- What is the
ReadableInterface? - Common Methods
- Examples of Using the
ReadableInterface - Conclusion
1. What is the Readable Interface?
The Readable interface provides a method for reading characters into a CharBuffer. It is implemented by classes like Reader and Scanner.
2. Common Methods
read(CharBuffer cb): Reads characters into the specifiedCharBuffer.
3. Examples of Using the Readable Interface
Example 1: Implementing Readable in a Custom Class
This example demonstrates how to implement the Readable interface in a custom class.
import java.nio.CharBuffer;
public class CustomReadable implements Readable {
private String data;
private int currentPosition = 0;
public CustomReadable(String data) {
this.data = data;
}
@Override
public int read(CharBuffer cb) {
if (currentPosition >= data.length()) {
return -1; // End of input
}
cb.append(data.charAt(currentPosition++));
return 1; // Number of characters read
}
public static void main(String[] args) {
CustomReadable readable = new CustomReadable("Hello, World!");
CharBuffer buffer = CharBuffer.allocate(50);
while (readable.read(buffer) != -1) {
// Continue reading
}
buffer.flip();
System.out.println("Buffer content: " + buffer.toString());
}
}
Output:
Buffer content: Hello, World!
Example 2: Using Readable with Scanner
This example shows how to use Readable with a Scanner to read from a custom readable source.
import java.nio.CharBuffer;
import java.util.Scanner;
public class ReadableWithScannerExample {
public static void main(String[] args) {
Readable readable = CharBuffer.wrap("This is a test.");
Scanner scanner = new Scanner(readable);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
scanner.close();
}
}
Output:
This
is
a
test.
4. Conclusion
The Readable interface in Java provides a standard way to read character data into a CharBuffer. By implementing this interface, custom classes can be used as character sources in Java applications, allowing for flexible character input handling.
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