简体   繁体   English

如何在java中将字节数组保存到二进制文件中

[英]How to save byte array into an binary file in java

How to save byte array into an binary file in java. 如何在java中将字节数组保存到二进制文件中。 I have tries using file writer,bufferedOutputstream but with no results. 我尝试使用文件编写器,bufferedOutputstream但没有结果。 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. 数据保存在二进制文件中,当用记事本打开文件时,它看起来像二进制文件已经创建,但是当用wordpad打开时,会显示实际数据。

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 它将使用您的字节数组创建一个文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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