Reader transferTo(Writer) in Java

transferTo(Writer): This method is available in the java.io.Reader class of Java.

Syntax:

long java.io.Reader.transferTo(Writer out) throws IOException

This method takes one argument. This method reads all characters from this reader and writes the characters to the given writer in the order that they are read.

This method does not close either reader or writer.

Parameters: One parameter is required for this method.

out: the writer, non-null.

Returns: the number of characters transferred.

Throws:

1. IOException - if an I/O error occurs when reading or writing.

2. NullPointerException - if out is null.

Approach 1: When no exception

Java

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

public class ReadertransferTo {
    public static void main(String[] args) throws IOException {

        Reader reader = new StringReader("HELLO");

        Writer out = new StringWriter();

        System.out.println(reader.transferTo(out));
        reader.close();

    }
}

Output:

5


Approach 2: IOException 

Java

package com.Reader;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

public class ReadertransferTo {
    public static void main(String[] args) throws IOException {

        Reader reader = new StringReader("HELLO");

        Writer out = new StringWriter();

        reader.close();
        System.out.println(reader.transferTo(out));

    }
}

Output:

Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.StringReader.ensureOpen(StringReader.java:56) at java.base/java.io.StringReader.read(StringReader.java:91) at java.base/java.io.Reader.transferTo(Reader.java:384) at com.Reader.ReadertransferTo.main(ReadertransferTo.java:17)


Approach 3: NullPointerException

Java

package com.Reader;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;

public class ReadertransferTo {
    public static void main(String[] args) throws IOException {

        Reader reader = new StringReader("HELLO");

        Writer out = null;

        System.out.println(reader.transferTo(out));
        reader.close();

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: out at java.base/java.util.Objects.requireNonNull(Objects.java:233) at java.base/java.io.Reader.transferTo(Reader.java:380) at com.Reader.ReadertransferTo.main(ReadertransferTo.java:15)


Some other methods of Reader

close()This method closes the stream and releases any system resources associated with it.

mark(int)This method marks the present position in the stream.

markSupported()This method tells whether this stream supports the mark() operation. 

Reader.nullReader()This method returns a new Reader that reads no characters.

read()This method reads a single character.

read(char[])This method reads characters into an array.

read(CharBuffer)This method attempts to read characters into the specified character buffer.

read(char[], int, int)This method reads characters into a portion of an array.

ready()This method tells whether this stream is ready to be read.

reset()This method resets the stream.

skip(long)This method skips characters.

transferTo(Writer)This method reads all characters from this reader and writes the characters to the given writer in the order that they are read.

No comments:

Post a Comment