Java BufferedOutputStream class

Introduction

Buffers for output in Java can increase performance.

Here are the two available constructors:

BufferedOutputStream(OutputStream outputStream)  
BufferedOutputStream(OutputStream outputStream, int bufSize) 


import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class Main {

  public static void main(String[] args) throws Exception {
    BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream(
        "Main.txt"));
    bufferedOutput.write("Line one".getBytes());
    bufferedOutput.write("\n".getBytes());

    bufferedOutput.write(65);/*from  w  ww.  j  ava 2  s .  co  m*/
    bufferedOutput.close();
  }
}

Write 12 bytes to the file

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class Main {

  public static void main(String args[]) throws Exception {
    FileOutputStream fos = new FileOutputStream(args[0]);

    BufferedOutputStream bos = new BufferedOutputStream(fos);

    // Write 12 bytes to the file
    for (int i = 0; i < 12; i++) {
      bos.write(i);/*w  ww  .  j  a va2s  .com*/
    }

    bos.close();
  }
}



PreviousNext

Related