Java RandomAccessFile

1 什么是Java RandomAccessFile

Java RandomAccessFile类是用于读取和写入随机访问文件。随机访问文件的行为类似于大字节数组。有一个指向该数组的游标,称为文件指针,通过移动游标我们可以执行读写操作。如果在读取所需的字节数之前到达文件末尾,则抛出EOFException异常,它是IOException异常的一种。

2 Java RandomAccessFile的构造方法

构造方法 描述
RandomAccessFile(File file, String mode) 创建一个RandomAccessFile,以读取和可选地写入File参数指定的文件。
RandomAccessFile(String name, String mode) 创建一个RandomAccessFile,以读取具有指定名称的文件,也可以选择写入该文件。

3 Java RandomAccessFile的方法

方法 描述
void close() 此方法关闭此随机存取文件流并释放与该流关联的所有系统资源。
FileChannel getChannel() 此方法返回与此文件关联的唯一文件通道对象。
FileDescriptor getFD() 此方法返回与此流关联的不透明文件描述符对象。
long getFilePointer() 此方法返回当前在此文件中的偏移。
long length() 这个方法返回当前文件的长度。
int read() 此方法读取数据从该文件一个字节。
int read(byte[] b) 此方法读取为从该文件b.length个数据字节为字节数组。
int read(byte[] b, int off, int len) 此方法读取为从该文件len个字节数据到一个字节数组。
boolean readBoolean() 此方法读取该文件一个布尔值。
byte readByte() 此方法从该文件读取有符号8位的值。
char readChar() 此方法从文件读取一个字符。
double readDouble() 此方法从文件读取一个double数。
float readFloat() 此方法从文件读取一个浮点数。
void readFully(byte[] b) 此方法读取该文件b.length个字节到字节数组,并从当前文件指针。
void readFully(byte[] b, int off, int len) 此方法读取这个文件正好len个字节到字节数组,并从当前文件指针。
int readInt() 此方法从该文件中读取一个有符号的32位整数。
String readLine() 此方法从该文件中读取文本的下一行。
long readLong() 此方法从该文件中读取一个有符号的64位整数。
short readShort() 此方法从该文件中读取一个有符号的16位数。
int readUnsignedByte() 此方法从该文件中读取一个无符号的八位数。
int readUnsignedShort() 此方法从该文件中读取一个无符号的16位数。
String readUTF() 从这个文件中的字符串此方法读取。
void seek(long pos) 此方法设置文件指针偏移量,从这个文件开始测量,进行下一个读或写操作发生。
void setLength(long newLength) 此方法设置此文件的长度。
int skipBytes(int n) 此方法尝试跳过n个字节的输入丢弃跳过的字节。
void write(byte[] b) 此方法写入b.length个字节从指定的字节数组到该文件,并从当前文件指针。
void write(byte[] b, int off, int len) 此方法从指定的字节数组开始到该文件偏移量off写入len字节。
void write(int b) 此方法写入指定的字节写入此文件。
void writeBoolean(boolean v) 此方法写入一个布尔值,该文件为一个字节的值。
void writeByte(int v) 此方法写入一个字节到文件作为一个单字节值。
void writeBytes(String s) 此方法写入字符串到文件为一个字节序列。
void writeChar(int v) 此方法写入一个字符的文件作为一个双字节值,高字节在前。
void writeChars(String s) 此方法将一个字符串写入该文件作为一个字符序列。
void writeDouble(double v) 此方法double参数转换为long使用doubleToLongBits方法在类Double,然后写到long值的文件作为八字节数量,高字节在前。
void writeFloat(float v) 此方法float参数转换为使用floatToIntBits方法在类Float一个int,然后写到int值,以该文件为一个四字节数量,高字节在前。
void writeInt(int v) 此方法写入一个int到文件为四个字节,高字节在前。
void writeLong(long v) 此方法写入一个长的文件作为八个字节,高字节在前。
void writeShort(int v) 此方法写入一个短的文件为两个字节,高字节在前。
void writeUTF(String str) 这种方法将一个字符串写入使用经修订的UTF-8编码以与机器无关的方式的文件。

4 Java RandomAccessFile的例子

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 *  Java RandomAccessFile的例子
 */
import java.io.IOException;
import java.io.RandomAccessFile;

public class Demo {

    static final String FILEPATH ="file.txt";
    public static void main(String[] args) {
        try {
            System.out.println(new String(readFromFile(FILEPATH, 0, 18)));
            writeToFile(FILEPATH, "I love my country and my people", 31);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static byte[] readFromFile(String filePath, int position, int size)
            throws IOException {
        RandomAccessFile file = new RandomAccessFile(filePath, "r");
        file.seek(position);
        byte[] bytes = new byte[size];
        file.read(bytes);
        file.close();
        return bytes;
    }
    private static void writeToFile(String filePath, String data, int position)
            throws IOException {
        RandomAccessFile file = new RandomAccessFile(filePath, "rw");
        file.seek(position);
        file.write(data.getBytes());
        file.close();
    }
}

file.txt的原本内容为:

This class is used for reading and writing to random access file.

执行以上程序后,控制台输出结果为:

This class is used

file.txt的内容改为:

This class is used for reading I love my country and my peoplele.

 

热门文章

优秀文章