Which exception is thrown by read() method of inputstream class?
Answers
Answered by
1
Hey There!!
Short Answer: IOException
Long Answer:
JAVA has many systems and methods for taking input from a file.
JAVA has an abstract class, called InputStream.
This InputStream class is the superclass of many other subclasses, like FileInputStream, ByteInputStream, etc.
The InputStream class has three methods titled as read()
-> public abstract int read() throws IOException
This method reads the next byte of data from the input stream. It returns a integer value from 0 to 255. End Of File (EOF) is marked as -1.
It throws IOException if there's an error in reading from file.
-> public int read(byte[] b) throws IOException
This method reads bytes from the file and stores them into a buffer array b
It also throws IOException if an error has occured while reading from file.
-> public int read(byte[] b, int off, int len) throws IOException
This method starts reading data, and reads up to len bytes of data.
The bytes are stored in buffer array b, and storage starts from position b[off].
That is, the bytes are stored in b[off], b[off+1], b[off+2], etc
This method can throw three exceptions:
IOException - If first byte cannot be read for any reason, or any other IO error occurs
NullPointerException - If b is null
ArrayIndexOutOfBoundsException - If off is negative, or len is negative, or if len is greater than b.length-off
_______________________________________
In any case, there is one common exception, which is also your answer: IOException
Hope it helps
Purva
Brainly Community
Short Answer: IOException
Long Answer:
JAVA has many systems and methods for taking input from a file.
JAVA has an abstract class, called InputStream.
This InputStream class is the superclass of many other subclasses, like FileInputStream, ByteInputStream, etc.
The InputStream class has three methods titled as read()
-> public abstract int read() throws IOException
This method reads the next byte of data from the input stream. It returns a integer value from 0 to 255. End Of File (EOF) is marked as -1.
It throws IOException if there's an error in reading from file.
-> public int read(byte[] b) throws IOException
This method reads bytes from the file and stores them into a buffer array b
It also throws IOException if an error has occured while reading from file.
-> public int read(byte[] b, int off, int len) throws IOException
This method starts reading data, and reads up to len bytes of data.
The bytes are stored in buffer array b, and storage starts from position b[off].
That is, the bytes are stored in b[off], b[off+1], b[off+2], etc
This method can throw three exceptions:
IOException - If first byte cannot be read for any reason, or any other IO error occurs
NullPointerException - If b is null
ArrayIndexOutOfBoundsException - If off is negative, or len is negative, or if len is greater than b.length-off
_______________________________________
In any case, there is one common exception, which is also your answer: IOException
Hope it helps
Purva
Brainly Community
Similar questions