Search code examples
javafileoutputstream

FileOutputStream change `0A` byte to `0D 0A`


I convert float to byte[] array with this code:

byte[] bytes = ByteBuffer.allocate(4).putFloat(number).array();

For example if I put the number 0.02f I get the bytes [60,-93,-41,10] Then I try to write this byte[] to file with this code:

FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
dbf.close();

On most platform in HEX-editor this file looks like this: 3C A3 D7 0A. But on special device same code gives this: 3C A3 D7 0D 0A. Before each 0A 0D appears.

I know than 0A is a LF and 0D 0A is a CRLF but I do not know how this can be.

With what it can be connected ?


Solution

  • It sounds like when the data is transferred to your "special device" it is being translated as if it was a text file. This will corrupt the file in the manner you see. e.g. FTP has a text transfer mode which does this.

    You shouldn't confuse text and binary. You are writing a binary format and if you read it as a binary format, new lines are not relevant.

    Don't read it as text as it's not text and you won't have a problem.