6

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?

Bernhard Barker
  • 50,899
  • 13
  • 85
  • 122
esgeroth
  • 85
  • 7
  • 3
    Although I don't believe it will make a difference here, try to `.flush()` then `.close()` after you `.write()`... – fge Jun 14 '13 at 07:26
  • 1
    That's *very* strange. And that's the exact code you execute? No `Writer` anywhere? Or `String` values? Also: the `createNewFile()` call should not be necessary. – Joachim Sauer Jun 14 '13 at 07:51

2 Answers2

1

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.

3yakuya
  • 2,471
  • 3
  • 20
  • 38
  • This was the problem. I was using the hex editor plugin for notepad++ to read the file. I downloaded the HxD hex editor and now the file contents are read as expected. – esgeroth Jun 14 '13 at 17:01
0

I tested your code and it's not failing for me. Are you transfering your file between servers? As it is a .txt file, you might get some auto text conversion going on, have you tried without extension?

Also, make sure to close the resources when finished.

Martin
  • 2,894
  • 22
  • 37