A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-io-pipedreader-class-java/ below:

Java PipedReader Class - GeeksforGeeks

Java PipedReader Class

Last Updated : 22 Apr, 2025

The PipedReader class in Java is part of the java.io package, and it is used to read character data from a pipe. This class allows inter-thread communication, where one thread writes data using a PipedWriter, and another reads it using PipedReader.

Features of PipedReader Class:

What is a Pipe in Java?

In Java, a pipe is used to link two threads. One thread is used to send data through the pipe, and the other thread reads the data. If the thread that is sending the data stops or crashes, then the pipe is considered to be broken.

Declaration of PipedReader Class

The Declaration of the PipedReader class is:

public class PipedReader extends Reader

All Implemented Interfaces:

Constructors of PipedReader

This class consists of four constructors with the help of which we can create object of this class in different ways. The following are the constructors available in this class:

1. PipedReader(): This constructor creates a PipedReader that is not connected to any writer yet.

Syntax:

public PipedReader()

2. PipedReader(int pipeSize): This constructor creates aPipedREader with a specified pipe size.

Syntax:

public PipedReader(int pSize)

3. PipedReader(PipedWriter src): This constructor creates a PipedReader, that is connected to the PipedWriterStream src.

public PipedReader(PipedWriter src)

4. PipedReader(PipedWriter src, int pipeSize): This constructor creates a connected PipedReader with a specified size and linked to the given PipedWriter.

Syntax:

public PipedReader(PipedWriter src, int pSize)

Java PipedReader Methods

The image below demonstrates the methods of PipedReader class.

Now, we are going to discuss about each method one by one in detail:

1. read(): This method is used to get the next character from the PipedReader. It blocks until there is data to read ot an error occurs.

Syntax:

public int read() throws IOException

Example:

Java
// Demonstrating the working 
// of read() method
import java.io.*;

public class GeeKs {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        // Connect the reader and writer
        r.connect(w);

        // Write data to the PipedWriter
        w.write(71); 
        System.out.println("Read: " + (char) r.read()); 

        w.write(69);  
        System.out.println("Read: " + (char) r.read());  

        w.write(75); 
        System.out.println("Read: " + (char) r.read()); 
    }
}

Output
Read: G
Read: E
Read: K

2. read(char[] carray, int offset, int maxlen): This method is used to reads upto maxlen character from PipedReader Stream to the character array. The method blocks if end of Stream is reached or exception is thrown.

Syntax:

public int read(char[] carray, int offset, int maxlen) throws IOException

Example:

Java
// Demonstrating the working 
// of read(char[] carray, int offset, int maxlen) 
import java.io.*;

public class Geeks {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        r.connect(w);

        // Write data to PipedWriter
        w.write(71);  // G
        w.write(69);  // E
        w.write(75);  // K
        w.write(83);  // S

        // Read data into an array
        char[] b = new char[5];
        r.read(b, 0, 5);
        
        System.out.print("Read characters: ");
        for (char c : b) {
            System.out.print(c);  
        }
    }
}

Output
Read characters: GEKS

3. close(): This method is used to close the PipedReader.

Syntax:

public void close() throws IOException

Example:

Java
// Demonstrating the working
// of close() method
import java.io.*;

public class Geeks {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        r.connect(w);
        w.write(71); 

        // Close the reader
        r.close();
        System.out.println("Stream closed.");
    }
}

4. ready(): This method is used to check whether the stream is ready to be read.

Syntax:

public boolean ready() throws IOException

Example:

Java
// Demonstrating the working
// of ready() method
import java.io.*;

public class Geeks {
    
    public static void main(String[] args) throws IOException {
        
        PipedReader r = new PipedReader();
        PipedWriter w = new PipedWriter();

        r.connect(w);

        w.write(71);  

        // Check if the stream is ready to be read
        System.out.println("Stream is ready to be read: " + r.ready());
    }
}

Output
Stream is ready to be read: true

5. close(): This method is used to close the PipedReader streams.

Syntax:

public void close()

Example:

Java
// Demonstrating the working
// of close() method
import java.io.*;

public class Geeks{
    
    public static void main(String[] args) {
        
        try {
            // Create a PipedReader and PipedWriter
            PipedReader r = new PipedReader();
            PipedWriter w = new PipedWriter();

            // Connect the PipedReader to the PipedWriter
            r.connect(w);

            // Write a character to the PipedWriter
            w.write('A');
            
            // Read and print the character from the PipedReader
            System.out.println("Read: " + (char) r.read());  // Output: A

            // Close the PipedReader stream
            r.close();
            System.out.println("Stream closed.");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output
Read: A
Stream closed.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4