0

The following is the Java program. Write and Read operation is showing me correct value. But when I am reading the same binary file in C#.Net I am getting incorrect values.

JAVA PROGRAM

///////////////////////////////////////////////////////////////////////////
public class Main {
    public static void main(String[] args) {
        // ReadFile o = new ReadFile();
        // o.Read("csharp123.dat");

        WriteFile w = new WriteFile();
        w.Write("java123.dat");
        w = null;

        ReadFile r = new ReadFile();
        r.Read("java123.dat");
        r = null;
    }
}

/////////////////////////////////////////////////////////////////////
class WriteFile {
    Random m_oRN = new Random();

    private int getRandom()
    {
        return m_oRN.nextInt(100);
    }

    public void Write(String strFileName) {
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new FileOutputStream(strFileName));
            dos.writeInt(123);
            dos.writeInt(234);
            dos.writeInt(345);

            dos.flush();
            dos.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

///////////////////////////////////////////////////////////////////////////
//
class ReadFile {

    public void Read(String strFileName) {

        try {
            File fp = new File(strFileName);
            FileInputStream fis = new FileInputStream(fp);
            DataInputStream din = new DataInputStream(fis);

            int count = (int) (fp.length() / 4);
            for (int i = 0; i < count; i++) {
                System.out.println(din.readInt());
            }

            din.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

//--------------------------------------------------- C# PROGRAM

private void msViewRead_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    string strLastPath = RegistryAccess.ReadRegistryString("LastSavedPath");
    dlg.InitialDirectory = strLastPath.Length == 0 ? Application.StartupPath : strLastPath;
    dlg.Filter = "Data file (*.dat)|*.dat|All file (*.*)|*.*||";
    dlg.FilterIndex = 1;
    if (dlg.ShowDialog() != DialogResult.OK)
        return;

    this.Cursor = Cursors.WaitCursor;

    string strValue = String.Empty;

    using (var fs = File.Open(dlg.FileName, FileMode.Open))
    using (var br = new BinaryReader(fs))
    {
        var vPos = 0;
        var vLen = (int)br.BaseStream.Length;
        while (vPos < vLen)
        {
             strValue += br.ReadInt32();
             strValue += '\n';
             vPos += sizeof(int);
        }
    }

    this.Cursor = Cursors.Default;

    MessageBox.Show(strValue, "Read Binary File");

}

Here I am getting the junk data.

S G
  • 651
  • 6
  • 10
  • On windows binary files are fundamentally different from text files. The first thing I would check is that the Java program is definitely creating a binary type file, not a text file. If you can open the file in a text editor and see the data you wrote in Java then the binary data will not reflect the text data. – Kelson Ball Oct 17 '16 at 15:49
  • 2
    Then you want to check the existing documentation to understand the *format* that the Java side is applying, for example: https://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html ... you see that is the point of *binary*: every single bit matters. If you want to read binary data, you have to understand each and any bit that goes in there; and the ways how information is encoded. – GhostCat Oct 17 '16 at 15:49
  • What values you get? – talex Oct 17 '16 at 15:51

1 Answers1

2

Most probably your issue is with the endianness. DataOutputStream writes the high byte first (Big Endian). .NET's BinaryReader expects high byte last (Little Endian). You need a way to reverse the byte order. See here for an example on how to do that.

Community
  • 1
  • 1
Hintham
  • 1,078
  • 10
  • 29