Write byte array to file using BufferedOutputStream - Java File Path IO

Java examples for File Path IO:BufferedOutputStream

Description

Write byte array to file using BufferedOutputStream

Demo Code


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {

  public static void main(String[] args) {

    String strFileName = "C:/Folder/Demo";
    BufferedOutputStream bos = null;

    try {/*w  w  w .ja  v a2s  .c  om*/
      FileOutputStream fos = new FileOutputStream(new File(strFileName));
      bos = new BufferedOutputStream(fos);

      String str = "Example";

      bos.write(str.getBytes());

    } catch (FileNotFoundException fnfe) {
      System.out.println("Specified file not found" + fnfe);
    } catch (IOException ioe) {
      System.out.println("Error while writing file" + ioe);
    } finally {
      if (bos != null) {
        try {

          // flush the BufferedOutputStream
          bos.flush();

          // close the BufferedOutputStream
          bos.close();

        } catch (Exception e) {
        }
      }
    }

  }
}

Result


Related Tutorials