Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing byte array to file. Not always getting expected result

I am using Java to write a byte array to a file. When I open my file in a hex editor I am not always seeing the bytes that I expected to be there. Here is my example code and contents of the output file:

public static void main(String[] args) 
{
    File file = new File( "c:\\temp\\file.txt" );
    file.delete();
    FileOutputStream outStream = null;
    try 
    {
        file.createNewFile();
    } catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try 
    {
        outStream = new FileOutputStream( file );
    } catch (FileNotFoundException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try 
    {
    outStream.write( new byte[] { 0x14, 0x00, 0x1F, 0x50 } );

    } catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

When I open the file in a hex editor I get 00 9e f3 04 as the contents instead of the bytes that I sent. My results seem to be inconsistent. Sometimes I get the expected result and sometimes I do not.


This will output the correct data:

outStream.write( new byte[] { 0x14 , 0x00, 0x1F, 0x50, (byte) 0xE0, 0x4F, (byte) 0xD0, 0x20, (byte) 0xEA, 0x3A, 0x69, 0x10, (byte) 0xA2 , (byte) 0xD8, 0x08, 0x00, 0x2B } );

File contents are:

14 00 1f 50 e0 4f d0 20 ea 3a 69 10 a2 d8 08 00 2b

If I add one more byte to that array then it fails.

outStream.write( new byte[] { 0x14 , 0x00, 0x1F, 0x50, (byte) 0xE0, 0x4F, (byte) 0xD0, 0x20, (byte) 0xEA, 0x3A, 0x69, 0x10, (byte) 0xA2 , (byte) 0xD8, 0x08, 0x00, 0x2B, 0x30 } );

File contents are now:

14 e5 80 9f e4 bf a0 e2 83 90 e3 ab aa e1 81 a9 ed a2 a2 08 e3 80 ab

I also had trouble with this one:

outStream.write( new byte[] { 0x4C, 0x00, 0x00, 0x00 } );

File contents are:

4c 00

The last two bytes are not written.


outStream.write( new byte[] { 0x4C, 0x00, 0x00, 0x00, 0x01 } );

This will produce the expected result. File contents are:

4c 00 00 00 01

I feel like I am missing something fundamental about the way data is written to files. How can get consistent results when writing byte arrays to a file?

like image 486
esgeroth Avatar asked Jun 14 '13 07:06

esgeroth


1 Answers

I compiled exactly your code and in the output file I got what was expected. I think it is quite possible that your notepad (or any other program that you are using to check the file) is not showing you some of the bytes you write (For example, textedit on my Mac refuses to show a list of possible bytes). If this is exactly the method you are using I guess it is some other things (like notepad) that fail rather than your code. As you mentioned, sometimes you get the impression that some bytes are not written at all. Maybe trying the twin method public void write(byte[] b, int off, int len) can ensure you how many bytes are to be typed.

like image 82
3yakuya Avatar answered Nov 03 '22 09:11

3yakuya