Java IO Tutorial - Java OutputStream.write(byte[] b, int off, int len)








Syntax

OutputStream.write(byte[] b, int off, int len) has the following syntax.

public void write(byte[] b,  int off,  int len)  throws IOException

Example

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

/* w  w  w .j  a  v  a 2s .co m*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {

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

      OutputStream os = new FileOutputStream("test.txt");

      InputStream is = new FileInputStream("test.txt");

      os.write(b, 0, 3);

      for (int i = 0; i < 3; i++) {
        System.out.print((char) is.read());
      }
      os.close();
      is.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }

  }
}

The code above generates the following result.