简体   繁体   English

如何在Java中将字节数组写入二进制文件

[英]How to write array of bytes to a binary file in Java

How to write an array of bytes b[i] to a binary file in Java. 如何用Java将字节数组b[i]写入二进制文件。

I need to write those bytes it into a "binary file" to be able to read it later using hex editor (AXE). 我需要将那些字节写入“二进制文件”,以便以后使用十六进制编辑器(AXE)读取它。

Some readers might be confused by "binary file", by binary file I don't mean a file filled by zeros and ones, I mean machine-readable form, something like this : binary files in text editor 某些读者可能会对“二进制文件”感到困惑,对于二进制文件,我不是指用零和一填充的文件,而是指机器可读的形式,例如: 文本编辑器中的二进制文件

The hex editor suppose to read this data, hex editor 十六进制编辑器假定要读取此数据, 十六进制编辑器

From what I understand I need to byte stream that data into a file Is there a command I could use for this purpose. 据我了解,我需要将数据按字节流式传输到文件中,是否可以使用此命令。

Any code would be appreciated. 任何代码将不胜感激。

Just write the byte[] to a FileOutputStream pointing to the file: 只需将byte []写入指向文件的FileOutputStream即可:

private static void writeBytesToFile(byte[] b, String f) {

    try (FileOutputStream out = new FileOutputStream(f)){
        out.write(b);
    }

    catch (IOException e) {
        e.printStackTrace();
    }          
}

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

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