Write double to a file using DataOutputStream
- /*
- Write double to a file using DataOutputStream
- This Java example shows how to write a Java double primitive value to a file using
- writeDouble method of Java DataOutputStream class.
- */
- import java.io.DataOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class WriteDoubleToFile {
- public static void main(String[] args) {
- String strFilePath = "C://FileIO//WriteDouble.txt";
- try
- {
- //create FileOutputStream object
- FileOutputStream fos = new FileOutputStream(strFilePath);
- /*
- * To create DataOutputStream object from FileOutputStream use,
- * DataOutputStream(OutputStream os) constructor.
- *
- */
- DataOutputStream dos = new DataOutputStream(fos);
- double d = 165;
- /*
- * To write a double value to a file, use
- * void writeDouble(double d) method of Java DataOutputStream class.
- *
- * This method writes specified double to output stream as 8 bytes value.
- * Please note that the double value is first converted to long using
- * Double.doubleToLongBits method and then long is written to
- * underlying output stream.
- */
- dos.writeDouble(d);
- /*
- * To close DataOutputStream use,
- * void close() method.
- *
- */
- dos.close();
- }
- catch (IOException e)
- {
- System.out.println("IOException : " + e);
- }
- }
- }



