问题
I have a problem with FileOutputStream.
I use this simple code:
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();
File is written correctly on most platforms (Windows, Android). In HEX-editor it looks like this:
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:
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;
}
Before sending array to FileOutputStream an array looks like this (get from debug):
How can this be and how can I fix this ?
回答1:
As it turned out, all code works correctly. Mail client broke the file during sending.
来源:https://stackoverflow.com/questions/43499255/fileoutputstream-write-non-existent-bytes