如何用Java将字节数组写入文件

如何用Java将字节数组写入文件,java,file,bytearray,Java,File,Bytearray,我想在文件中写入一个字节数组。这是网络上的代码 我转换成了java: byte [] header= { 0x2e736e64, 24, 0xffffffff, 1, 8000, 1 }; FileOutputStream out = new FileOutputStream(file); out.write(header); 但这是错误的。你能帮我修一下吗。谢谢在您的python代码中,您正在写入32位整数,而不是8位字节; 您可

我想在文件中写入一个字节数组。这是网络上的代码

我转换成了java:

            byte []  header= {   0x2e736e64, 24, 0xffffffff, 1, 8000, 1 };
        FileOutputStream out = new FileOutputStream(file);
        out.write(header);

但这是错误的。你能帮我修一下吗。谢谢

在您的python代码中,您正在写入32位整数,而不是8位字节; 您可以使用此代码获得相同的结果:

 byte []  header= {   0x2e, 0x73, 0x6e, 0x64,
                      0x0,  0x0,  0x0,  24,
                      -1,  -1,    -1,   -1,
                      0,   0,     0,    1,
                      0,   0,     0x1f, 0x40,
                      0,   0,     0,    1 };
 FileOutputStream out = new FileOutputStream(file);
 out.write(header);

您需要指定错误。提示:0x2e736e64不是字节的有效值…是的,我想将0x2e736e64写入字节数组。我正在使用字节0x2e736e64。但数据丢失了。ASCII是。snd@toanhoi检查
 byte []  header= {   0x2e, 0x73, 0x6e, 0x64,
                      0x0,  0x0,  0x0,  24,
                      -1,  -1,    -1,   -1,
                      0,   0,     0,    1,
                      0,   0,     0x1f, 0x40,
                      0,   0,     0,    1 };
 FileOutputStream out = new FileOutputStream(file);
 out.write(header);