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.