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

java io BufferedOutputStream Class

java.io.BufferedOutputStream Class

BufferedOutputStream Class in Java

A BufferedOutputStream is similar to any OutputStream with the exception of an added flush() method that is used to ensure that data buffers are written to the stream being buffered. Since the point of a BufferedOutputStream is to improve performance by reducing the number of times the system actually writes data, you may need to call flush() to cause any data that is in the buffer to be immediately written.

Unlike buffered input, buffering output does not provide additional functionality. Buffers for output in Java are there to increase performance.

java.io.BufferedOutputStream 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
/*  Java BufferedOutputStream class Example
    Following example shows how to read the text
    from file using java BufferedOutputStream.
    Save with file name BufferedOutputStreamExample.java    */
  
import java.io.*;
  
public class BufferedOutputStreamExample
{
  public static void main(String args[])
  {
    //BufferedOutputStream DECLARATION
    BufferedOutputStream bos = null;
    //FileOutputStream DECLARATION
    FileOutputStream fos = null;
    try
    {
      fos = new FileOutputStream("alphabets.txt");
      bos = new BufferedOutputStream(fos);
      //WRITES THE BYTES TO BufferedOutputStream
      for(int i=0;i<26;i++)
      {
        bos.write((byte) ('A'+i));
      }
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : "+e.getMessage());
    }
    finally
    {
      //SHOULD CLOSE STREAMS FINALLY
      try
      {
        if(bos!=null)
          bos.close();
        if(fos!=null)
          fos.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".

How to read total bytes from file into array

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
56
57
58
59
60
/*  How to read total bytes from file into array
    and writes to file using java BufferedOutputStream Example
    Save with file name BufferedOutputStreamExample2.java   */
  
import java.io.*;
  
public class BufferedOutputStreamExample2
{
  public static void main(String args[])
  {
    // BufferedInputStream DECLARATION
    BufferedInputStream bis = null;
    //FileInputStream DECLARATION
    FileInputStream fis = null;
    //ARRAY TO READ BYTES
    byte bytebuff[] = null;
    //BufferedOutputStream DECLARATION
    BufferedOutputStream bos = null;
    //FileOutputStream DECLARATION
    FileOutputStream fos = null;
    try
    {
      fis = new FileInputStream("BufferedOutputStreamExample2.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());
      fos = new FileOutputStream("duplicatefile.txt");
      bos = new BufferedOutputStream(fos);
      //WRITES THE BYTES TO BufferedOutputStream
      bos.write(bytebuff,0,bytebuff.length);
    }
    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();
        if(bos!=null)
          bos.close();
        if(fos!=null)
          fos.close();
      }
      catch(Exception ex)
      {
        System.out.println("Error Occurred : "+ex.getMessage());
      }
    }
  }
}
Download java io BufferedOutputStream Class in Formats