Java RandomAccessFile writeDouble()方法

java.io.RandomAccessFile.writeDouble(double v) 方法双参数使用Double类的doubleToLongBits方法转换为一个double值作为文件作为八字节数,高字节在前。写入从文件指针的当前位置。

1 语法

public final void writeDouble(double v)

2 参数

v:将要写出的double值。

3 返回值

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.RandomAccessFile.writeDouble(double v)方法的例子
 */
import java.io.*;

public class Demo {
    public static void main(String[] args) {

        try {
            double d = 1847.4986;

            // create a new RandomAccessFile with filename test
            RandomAccessFile raf = new RandomAccessFile("d:/test.txt", "rw");

            // write a double in the file
            raf.writeDouble(d);

            // set the file pointer at 0 position
            raf.seek(0);

            // read double
            System.out.println("" + raf.readDouble());

            // set the file pointer at 0 position
            raf.seek(0);

            // write a double at the start
            raf.writeDouble(473.5645);

            // set the file pointer at 0 position
            raf.seek(0);

            // read double
            System.out.println("" + raf.readDouble());

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

假设test.txt文件内容如下:

ABCDE  

输出结果为:

1847.4986
473.5645

热门文章

优秀文章