Hey
I was wondering how I could write a binary file in Java with code in C++ using the reinterpret_cast function. If I remember the line correctly (not currently at work) it was:
file.write=reinterpret_cast<char *(&x)>,sizeof x;
with x being a variable.
How can I do that in Java? From reading around, many topics said I should make the class Serializable. Would it be something like this?
public class anumber implements Serializable
{
private int x; /*Not sure if that is the correct way as I am not doing it in a IDE to declare it but know what i mean*/
public int getx()
{
return x;
}
public void setx(int y) //y being the variable i pass to this function
{
this.x=y;
}
}
then in another class
public class whatever
{
public void main()
{
anumber b=new anumber();
/*NEEDS HELP FROM THIS POINT TO WRITE THE NUMBER/VARIABLE x TO A BINARY FILE*/
}
}
Where I comment "/*NEEDS HELP FROM THIS POINT TO WRITE THE NUMBER/VARIABLE x TO A BINARY FILE*/" is obviously where I am stuck. Do I write the object b directly using the OutputStreamWriter (I belive it is) or do i use b.getx() and write that?
From what Ive been told the C++ code write bytes such as this:
10101010
11111111
92839423
from integers into the binary file.
How do I do this in Java giving the same output as the C++ program? Thank you
BTW: this is what reinterpret_cast does:
http://msdn.microsoft.com/en-us/library/e0w9f63b(v=VS.100).aspx