This section contains the detail about theWorking with binary files in java.
Working with binary files
In raw binary format, numerical data transfer is more faster than and compact than as text characters.
Writing Binary File
In the below example ,Firstly we create some data arrays containing some arbitrary values. After that we create object of FileOutputStream to open a stream to a file. We wrap this stream object with an instance of the DataOutputStream class, which contains writeInt (int i) and the writeDouble (double d) methods, to write the data to the file as pairs of int/double type values. It also contains many useful methods for writing primitive types of the writeX() form, where X indicates a primitive type.
Example :
import java.io.*; import java.util.*; public class BinaryFileWrite { public static void main(String arg[]) { Random ran = new Random(); // Create an integer array and a double array. int[] i_data = new int[15]; double[] d_data = new double[15]; // and fill them for (int i = 0; i < i_data.length; i++) { i_data[i] = i; d_data[i] = ran.nextDouble() * 10.0; } File file = null; // Get the output file name from the argument line. if (arg.length > 0) file = new File(arg[0]); // or use a default file name if (file == null) { System.out.println("Default: numerical.dat"); file = new File("numerical.dat"); } // Now write the data array to the file. try { // Create an output stream to the file. FileOutputStream file_output = new FileOutputStream(file); // Wrap the FileOutputStream with a DataOutputStream DataOutputStream data_out = new DataOutputStream(file_output); // Write the data to the file in an integer/double pair for (int i = 0; i < i_data.length; i++) { data_out.writeInt(i_data[i]); data_out.writeDouble(d_data[i]); } // Close file when finished with it.. file_output.close(); } catch (IOException e) { System.out.println("IO exception = " + e); } } // main }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>java BinaryFileWrite Default: numerical.dat |
Reading Binary File
In this example, firstly , we create some data arrays containing some arbitrary values. After that we create object of FileOutputStream class to open a stream to a file. We wrap this stream object with an instance of the DataOutputStream class, which contains writeInt (int i) and the writeDouble (double d) methods, to write the data to the file as pairs of int/double type values. It also contains many useful methods for writing primitive types of the writeX( ) form, where X indicates a primitive type.
Example :
import java.io.*; public class BinaryFileRead { public static void main(String arg[]) { File file = null; int i_data = 0; double d_data = 0.0; // Get the file from the argument line. if (arg.length > 0) file = new File(arg[0]); if (file == null) { System.out.println("Default: numerical.dat"); file = new File("numerical.dat"); } try { // Wrap the FileInputStream with a DataInputStream FileInputStream file_input = new FileInputStream(file); DataInputStream data_in = new DataInputStream(file_input); while (true) { try { i_data = data_in.readInt(); d_data = data_in.readDouble(); } catch (EOFException eof) { System.out.println("End of File"); break; } // Print out the integer, double data pairs. System.out.printf("%3d. Data = %8.3e %n", i_data, d_data); } data_in.close(); } catch (IOException e) { System.out.println("IO Exception =: " + e); } } // main }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>java BinaryFileRead Default: numerical.dat 0. Data = 9.418e+00 1. Data = 1.611e+00 2. Data = 6.298e+00 3. Data = 5.124e+00 4. Data = 2.766e-01 5. Data = 7.026e+00 6. Data = 8.160e+00 7. Data = 3.438e+00 8. Data = 1.587e+00 9. Data = 4.521e+00 10. Data = 4.027e-01 11. Data = 7.058e-01 12. Data = 3.245e+00 13. Data = 8.602e+00 14. Data = 8.451e+00 End of File |
[ 0 ] Comments