Download java io BufferedInputStream Class in Formats
QR Code for this Page
java io BufferedInputStream Class

java io BufferedInputStream Class

java.io.BufferedInputStream Class

BufferedInputStream Class in Java

A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. The internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.

Buffering I/O is a very common performance optimization. Java’s BufferedInputStream class allows you to "wrap" any InputStream into a buffered stream to improve performance.

java.io.BufferedInputStream class Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*  Java BufferedInputStream class Example
    Following example shows how to read the text
    from file using java BufferedInputStream.
    Save with file name BufferedInputStreamExample.java */
  
import java.io.*;
  
public class BufferedInputStreamExample
{
  public static void main(String args[])
  {
    //BufferedInputStream DECLARATION
    BufferedInputStream bis = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    try
    {
      int ch=0;
      fis = new FileInputStream("BufferedInputStreamExample.java");
      bis = new BufferedInputStream(fis);
      while((ch = bis.read()) != -1)
      {
        //CONVERT byte TO char
        System.out.print( (char) ch);
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        if(bis!=null)
          bis.close();
        if(fis!=null)
          fis.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

The above program output may look a little different from what you expected because System.in is line buffered, by default. This means that no input is actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive console input.

The following program demonstrates BufferedReader and the readLine() method; the program reads and displays lines of text until you enter the word "stop".

java.io.BufferedInputStream class Example 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*  Java BufferedInputStream class Example 2
    Following example shows how to read total bytes
    from file into array using java BufferedInputStream.
    Save with file name BufferedInputStreamExample2.java    */
  
import java.io.*;
  
public class BufferedInputStreamExample2
{
  public static void main(String args[])
  {
    //java.io.BufferedInputStream DECLARATION
    java.io.BufferedInputStream bis = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    //ARRAY TO READ BYTES
    byte bytebuff[] = null;
    try
    {
      fis = new FileInputStream("BufferedInputStreamExample2.java");
      bis = new BufferedInputStream(fis);
      //GET THE AVAILABLE BYTES FROM InputReader AND CREATES ARRAY
      bytebuff = new byte[fis.available()];
      //READ TOTAL BYTES FROM FILE
      bis.read(bytebuff, 0,fis.available());
      //PRINT CHARACTERS FROM ARRAY
      for(int j=0;j<bytebuff.length;j++)
      {
        //CONVERT byte TO char
        System.out.print( (char) bytebuff[j]);
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        //RELEASE ARRAY MEMORY
        bytebuff = null;
        if(bis!=null)
          bis.close();
        if(fis!=null)
          fis.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}

How to read total bytes from file into array using java BufferedInputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*  How to read total bytes from file into array
    using java BufferedInputStream Example
    Save with file name BufferedInputStreamExample3.java    */
  
import java.io.*;
  
public class BufferedInputStreamExample3
{
  public static void main(String args[])
  {
    String str = "This is a © copyright symbol but not © not.\n";
    //java.io.BufferedInputStream DECLARATION
    java.io.BufferedInputStream bis = null;
    //java.io.ByteArrayInputStream DECLARATION
    ByteArrayInputStream bais = null;
    //ARRAY TO READ BYTES
    byte bytebuff[] = null;
    try
    {
      bytebuff = str.getBytes();
      bais = new ByteArrayInputStream(bytebuff);
      bis = new BufferedInputStream(bais);
      while(
      {
        //CONVERT byte TO char
        System.out.print( (char) bytebuff[j]);
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        //RELEASE ARRAY MEMORY
        bytebuff = null;
        if(bis!=null)
          bis.close();
        if(fis!=null)
          fis.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}
Download java io BufferedInputStream Class in Formats