ready(): This method is available in the java.io.InputStreamReader class of Java.
Syntax:
boolean java.io.InputStreamReader.ready() throws IOException
This method tells whether this stream is ready to be read. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream.
Parameters: NA
Returns: True if the next read() is guaranteed not to block for input, false otherwise.
Note that returning false does not guarantee that the next read will block.
Throws:
IOException - If an I/O error occurs
Approach 1: When no exception
Java
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class InputStreamReaderready {public static void main(String[] args) throws IOException {File file = new File("D:\\hello.txt");InputStream in = new FileInputStream(file);InputStreamReader inputStreamReader = new InputStreamReader(in);try {System.out.println(inputStreamReader.ready());} catch (IOException e) {System.out.println("IOException occurs");}inputStreamReader.close();}}
Output:
false
Approach 2: IOException
Java
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class InputStreamReaderready {public static void main(String[] args) throws IOException {File file = new File("D:\\hello.txt");InputStream in = new FileInputStream(file);InputStreamReader inputStreamReader =new InputStreamReader(in);inputStreamReader.close();try {System.out.println(inputStreamReader.ready());} catch (IOException e) {System.out.println("IOException occurs");}}}
Output:
IOException occurs
Some more methods of InputStreamReader
close(): This method closes the stream and releases any system resources associated with it.
InputStreamReader(InputStream): This method creates an InputStreamReader that uses the default charset.
InputStreamReader(InputStream, Charset): This method creates an InputStreamReader that uses the given charset.
InputStreamReader(InputStream, CharsetDecoder): This method creates an InputStreamReader that uses the given charset decoder.
InputStreamReader(InputStream, String): This method creates an InputStreamReader that uses the named charset.
getEncoding(): This method returns the name of the character encoding being used by this stream.
read(): This method reads a single character.
read(char[], int, int): This method reads characters into a portion of an array.
No comments:
Post a Comment