Java - InputStream read() method
Description
The Java InputStream read() method reads the next byte of the data from the the input stream and returns int in the range of 0 to 255. If no byte is available because the end of the stream has been reached, the returned value is -1.
Declaration
Following is the declaration for java.io.InputStream.read() method −
public abstract int read()
Parameters
NA
Return Value
This method returns the next byte of data, or -1 if the end of the stream is reached.
Exception
IOException − If an I/O error occurs.
Example - Usage of InputStream read() method
The following example shows the usage of Java InputStream read() method.
InputStreamDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.InputStream;
public class InputStreamDemo {
public static void main(String[] args) throws Exception {
InputStream is = null;
int i;
char c;
try {
// new input stream created
is = new FileInputStream("test.txt");
System.out.println("Characters printed:");
// reads till the end of the stream
while((i = is.read())!=-1) {
// converts integer to character
c = (char)i;
// prints character
System.out.print(c);
}
} catch(Exception e) {
// if any I/O error occurs
e.printStackTrace();
} finally {
// releases system resources associated with this stream
if(is!=null)
is.close();
}
}
}
Output(Assuming test.txt contains "ABCDE")
Let us compile and run the above program, this will produce the following result−
Characters printed: ABCDE
Example - Reading One Byte at a Time Using FileInputStream
The following example shows the usage of Java InputStream read() method.
InputStreamDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamDemo {
public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("example.txt")) {
int data;
while ((data = inputStream.read()) != -1) { // Read byte by byte
System.out.print((char) data); // Convert byte to char and print
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(if example.txt contains "Hello")
Let us compile and run the above program, this will produce the following result−
Hello
Explanation
Uses FileInputStream, a subclass of InputStream, to read "example.txt".
Reads one byte at a time using read().
Converts the integer ASCII/Unicode value to a character((char) data) and prints it.
Stops when read() returns -1 (EOF reached).
Example - Reading One Byte at a Time Using ByteArrayInputStream
The following example shows the usage of Java InputStream read() method.
InputStreamDemo.java
package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamDemo throws IOException {
public static void main(String[] args) {
byte[] data = "Java".getBytes(); // Convert string to byte array
InputStream inputStream = new ByteArrayInputStream(data);
int byteRead;
while ((byteRead = inputStream.read()) != -1) { // Read one byte at a time
System.out.print((char) byteRead);
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Java
Explanation
Uses ByteArrayInputStream, which works with byte arrays instead of files.
Converts the string "Java" to bytes ("Java".getBytes()).
Reads one byte at a time and prints the character.
Closes the stream manually to free memory.