简体   繁体   中英

How to save byte array into an binary file in java

How to save byte array into an binary file in java. I have tries using file writer,bufferedOutputstream but with no results. The data gets saved in the binary file, when the file is opened with notepad, it looks like binary file has been created but when opened with wordpad the actual data appears.

You can save byte array in this way:

  FileOutputStream fos = new FileOutputStream(strFilePath);
  String strContent = "Content";

  /*
   * To write byte array to a file, use
   * void write(byte[] bArray) method of Java FileOutputStream class.
   *
   * This method writes given byte array to a file.
   */

   fos.write(strContent.getBytes());

  /*
   * Close FileOutputStream using,
   * void close() method of Java FileOutputStream class.
   *
   */

   fos.close();

It will create a file with your byte array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM