Java OutputStream.write(byte[] b)

Syntax

OutputStream.write(byte[] b) has the following syntax.

public void write(byte[] b)  throws IOException

Example

In the following code shows how to use OutputStream.write(byte[] b) method.


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
// w  w w .  ja  va 2  s . c  o m
public class Main {

  public static void main(String[] args) {
    byte[] b = { 'h', 'e', 'l', 'l', 'o' };
    try {

      // create a new output stream
      OutputStream os = new FileOutputStream("test.txt");

      // craete a new input stream
      InputStream is = new FileInputStream("test.txt");

      // write something
      os.write(b);

      // read what we wrote
      for (int i = 0; i < b.length; i++) {
        System.out.print((char) is.read());
      }
      os.close();
      is.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }

  }
}

The code above generates the following result.