FileOutputStream写入不存在的字节 [英] FileOutputStream write non-existent bytes
问题描述
我对FileOutputStream有问题.
I have a problem with FileOutputStream.
我使用以下简单代码:
byte[] data1 = BD25Writer.getData1();
byte[] data2 = BD25Writer.getData2();
FileOutputStream dbf = new FileOutputStream(new File(file.getPath(), "file1.dbf"));
dbf.write(data1);
dbf.close();
FileOutputStream dbt = new FileOutputStream(new File(file.getPath(), "file2.dbt"));
dbt.write(data2);
dbt.close();
在大多数平台(Windows,Android)上都可以正确写入文件.在十六进制编辑器中,它看起来像这样:
File is written correctly on most platforms (Windows, Android). In HEX-editor it looks like this:
但是在仅一台设备(YotaPhone2,型号:YD201,Android 6.0.1)上,此具有相同字节的代码无法正常工作.写入文件的不存在的字节:
But on only one device (YotaPhone2, model:YD201, android 6.0.1) this code with the same bytes is not working properly. Non-existent bytes written to the file:
方法.getData1()和.getData2()正常工作.这些方法只是将浮点数据写入字节数组. Сode看起来像这样:
Methods .getData1() and .getData2() works correctly. These methods simply write float data into an array of bytes. Сode looks like this:
private static byte[] getData1(){
int key = ...;
float[] buffer = new float[]{...};
byte[] array = new byte[buffer.length * 4];
byte[] bytes = new byte[4];
for (int i = 0; i < buffer.length; i++) {
bytes = ByteBuffer.allocate(4).putFloat(buffer[i]).array();
array[key + i * 4] = bytes[3];
array[key + i * 4 + 1] = bytes[2];
array[key + i * 4 + 2] = bytes[1];
array[key + i * 4 + 3] = bytes[0];
}
return array;
}
在将数组发送到FileOutputStream之前,数组如下所示(从调试中获取):
Before sending array to FileOutputStream an array looks like this (get from debug):
怎么可能?我该如何解决?
How can this be and how can I fix this ?
推荐答案
事实证明,所有代码都能正常工作.邮件客户端在发送过程中破坏了文件.
As it turned out, all code works correctly. Mail client broke the file during sending.
这篇关于FileOutputStream写入不存在的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!