Java RandomAccessFile writeLong()方法

java.io.RandomAccessFile.writeLong(long v) 方法写入一个长的文件作为八个字节,高字节在前。写入从文件指针的当前位置。

1 语法

public final void writeLong(long v)

2 参数

v:要写入一个long值。

3 返回值

4 示例 

package com.yiidian;

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

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

        try {
            long f = 184750874576l;

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

            // write a long in the file
            raf.writeLong(f);

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

            // read long
            System.out.println("" + raf.readLong());

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

            // write a long at the start
            raf.writeLong(20000000000l);

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

            // read long
            System.out.println("" + raf.readLong());

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

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

ABCDE  

输出结果为:

184750874576
20000000000

热门文章

优秀文章