Reader close() in Java

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

Syntax:

void java.io.Reader.close() throws IOException

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

Once the stream has been closed, further read(), ready(),mark(), reset(), or skip() invocations will throw an IOException.

Note: Closing a previously closed stream has no effect.

Parameters: NA

Returns: NA

Throws:

IOException - If an I/O error occurs

Approach

Java

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

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

        Reader reader = new FileReader("D:\\hello.txt");

        reader.close();

        System.out.println("Closed the reader");
    }
}

Output:

Closed the reader


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