Java.io.ObjectOutputStream.writeDouble() 方法



描述

java.io.ObjectOutputStream.writeDouble(double val) 方法寫入一個64位的雙精度浮點數。

宣告

以下是java.io.ObjectOutputStream.writeDouble() 方法的宣告。

public void writeDouble(double val)

引數

val − 要寫入的雙精度浮點數值。

返回值

此方法不返回值。

異常

IOException − 如果在寫入底層流時發生I/O錯誤。

示例

以下示例演示了java.io.ObjectOutputStream.writeDouble() 方法的用法。

package com.tutorialspoint;

import java.io.*;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      double d = 1.59875d;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeDouble(d);
         oout.writeDouble(9673.49764d);

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print what we wrote before
         System.out.println("" + ois.readDouble());
         System.out.println("" + ois.readDouble());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

1.59875
9673.49764
java_io_objectoutputstream.htm
廣告
© . All rights reserved.