Java RandomAccessFile readInt()方法

java.io.RandomAccessFile.readInt() 方法从该文件中读取一个有符号的32位整数。这个方法从文件中读取4个字节,并从当前文件指针。

1 语法

public final int readInt()

2 参数

3 返回值

此方法返回这个文件的下四个字节,解释为一个int。

4 示例 

package com.yiidian;

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

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

        try {
            int i = 284;

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

            // write something in the file
            raf.writeInt(123);

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

            // print the int
            System.out.println("" + raf.readInt());

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

            // write something in the file
            raf.writeInt(i);

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

            // print the int
            System.out.println("" + raf.readInt());

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

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

ABCDE  

输出结果为:

123
284

热门文章

优秀文章