
I need some help about Java programming. Please explain me "How to write in Binary file in Java programming language?"

Dear Friends,
Before writing file we need to know what is binary file in Java. The binary file contains bit patterns data type, which residnes in bit form. Before using the binary file we need to create some data arrays that should contain some value. For binary file FileOutputStream class is used for opened for the file and wrapped this stream object to BufferedOutputStream instance and then finally wrapped this stream object into the instance of DataOutputStream class.
For more details visit : [http://www.roseindia.net/java/examples/io/writeToFileBinary.shtml][1]
or
Find the Example or Syntax
import java.io.*;
class WriteToFileBinary
{
public static void main(String args[])
{
WriteToFileBinary wtfb = new WriteToFileBinary();
wtfb.writeToFileBinary();
}
public void writeToFileBinary()
{
String [] str ={ "A", "B", "C", "D"};
int[] in = { 12, 8, 13, 29, 50 };
double[] dob = { 19.99, 9.99, 15.99, 3.99, 4.99 };
FileOutputStream fos = null;
BufferedOutputStream bos = null;
DataOutputStream dos = null;
try
{
fos = new FileOutputStream("writeToFileBinary");
bos = new BufferedOutputStream(fos);
dos = new DataOutputStream(bos);
for (int i = 0; i < str.length; i ++)
{
dos.writeDouble(dob[i]);
dos.writeInt(in[i]);
dos.writeUTF(str[i]);
}
dos.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
[1]: http://www.roseindia.net/java/examples/io/writeToFileBinary.shtml
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.