
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - BufferedInputStream read() method
Description
The Java BufferedInputStream read() method reads the next byte of data from the input stream.
Declaration
Following is the declaration for java.io.BufferedInputStream.read() method.
public int read()
Parameters
NA
Return Value
This method does not return any value.
Exception
IOException − If an I/O error occurs.
Assumption
Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −
ABCDE
Example - Using read() method to read from File
The following example shows the usage of Java BufferedInputStream read() method.
BufferedInputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; public class BufferedInputStreamDemo { public static void main(String[] args) throws Exception { InputStream inStream = null; BufferedInputStream bis = null; try { // open input stream test.txt for reading purpose. inStream = new FileInputStream("example.txt"); // input stream is converted to buffered input stream bis = new BufferedInputStream(inStream); // read until a single byte is available while(bis.available()>0) { // read the byte and convert the integer to character char c = (char)bis.read(); // print the characters System.out.println("Char: "+c);; } } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases any system resources associated with the stream if(inStream!=null) inStream.close(); if(bis!=null) bis.close(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Char: A Char: B Char: C Char: D Char: E
Example - Using read() method to Read One Byte at a Time
The following example shows the usage of Java BufferedInputStream read() method.
BufferedInputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; public class BufferedInputStreamDemo { public static void main(String[] args) { byte[] data = "Hello, BufferedInputStream!".getBytes(); // open input stream test.txt for reading purpose. try (BufferedInputStream bis = new BufferedInputStream( new ByteArrayInputStream(data))) { System.out.println("Reading data one byte at a time:"); int byteData; while ((byteData = bis.read()) != -1) { // Read one byte at a time System.out.print((char) byteData); // Convert byte to char for printing } } catch (Exception e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
Reading data one byte at a time: Hello, BufferedInputStream!
Explanation
Stream Source− A BufferedInputStream wraps a ByteArrayInputStream that contains the byte array data.
-
Behavior−
The read() method reads one byte at a time and returns its integer value.
When the end of the stream is reached, read() returns -1.
Output: The characters are printed to the console one at a time.