Java FileOutputStream write byte array to file

Description

Java FileOutputStream write byte array to file

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

public class Main {
  public static void main(String[] args) {
    try (FileOutputStream fos = new FileOutputStream("Main.java")) {
      fos.write(new byte[] {64,65,66,67});
      fos.write("demo2s".getBytes());
      // Flush the written bytes to the file
      fos.flush();//from  w  ww  .ja v a 2 s  . c om
      System.out.println("saved");
    } catch (IOException e2) {
      e2.printStackTrace();
    }
  }
}



PreviousNext

Related