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








Syntax

FileOutputStream.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 FileOutputStream.write(byte[] b, int off, int len) method.

//www  . j  a  va 2 s. c om

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {

    byte[] b = { 65, 66, 67, 68, 69 };
    int i = 0;

    FileOutputStream fos = new FileOutputStream("C://test.txt");

    // writes byte to the output stream
    fos.write(b, 2, 3);

    // flushes the content to the underlying stream
    fos.flush();

    // create new file input stream
    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
      // convert integer to character
      char c = (char) i;
      System.out.print(c);
    }
  }
}