Signup/Sign In
PUBLISHED ON: MARCH 17, 2021

PipedReader read() method in Java

In this tutorial, we will learn about the read() method of PipedReader class in Java. This method reads the next character of data from this piped stream. If no character is available because the end of the stream has been reached, the value -1 is returned.

Syntax

This is the syntax declaration of the read() method, this method does not accept any parameter but returns an ASCII code in the integer format.

public int read()

Example: Read Char using PipedReader Class

In this example, we are going to implement read() method from the PipedReeader class. Firstly, we created objects for both the classes PipedReader and PipedWriter, and then connect them using connect() method. After this, we are simply writing and reading using the read() and write() method of PipedReader and PipedWriter class.

import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PipedReader reader = new PipedReader(); 
			PipedWriter writer = new PipedWriter(); 
			reader.connect(writer); 			
			writer.write(72); 
			System.out.println((char)reader.read()); 
			writer.write(69); 
			System.out.println((char)reader.read()); 
			writer.write(76); 
			System.out.println( (char)reader.read());   
			writer.write(76); 
			System.out.println( (char)reader.read());   
			writer.write(79); 
			System.out.println( (char)reader.read());   
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}

H E L L O

Example: Read char using the PipedReader Class

Here is a simple Java PipedReader example, we are reading data from the array of characters. As we stated earlier we must connect PipedReader with PipedWriter, in this program, we are connecting PipedWriter writer using the connect() method.

After connecting it, we are using the overloaded method public int read(char[] arr, int offset, int maxlen), here arr is the source of data,

offset indicates the starting position of the source from where we will start reading data. maxlength is the length of the data to be read.

For each call of this method, it will return a character on that index. When the index goes to the end of the source stream this method will return -1.

import java.io.PipedReader;
import java.io.PipedWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			PipedReader reader = new PipedReader(); 
			PipedWriter writer = new PipedWriter(); 
			reader.connect(writer); 
			char[] arr = {'H', 'E', 'L', 'L', 'O'}; 
			writer.write(arr, 0, 5); 
			while(true) 
			{ 
				System.out.print((char) reader.read());           
			} 
		}
		catch(Exception e)
		{
			System.out.println("Error: "+e.toString());
		}
	}
}

HELLO

Conclusion:

In this tutorial, we learned about the read() method of PipedReader class in Java. This method reads the next character of data from this piped stream. If no character is available because the end of the stream has been reached, the value -1 is returned.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.