其他分享
首页 > 其他分享> > RandomAccessFile 类的使用——基本使用

RandomAccessFile 类的使用——基本使用

作者:互联网

该类继承的是Object类,而不是任何流类,该类支持读取和写入(读写一体)随机访问文件(这里随机指的是可以自定义位置读写)。

相关方法

//构造方法
/*mode模式,常用的有两种:rw打开以便读取和写入; r以只读方式打开*/
RandomAccessFile(File file, String mode);
RandomAccessFile(String name, String mode);

//成员方法,除了以下几个方法,可以在read和write加上基本类型的符号,表示针对某一类型进行读写
int read(); //读取一个数据字节
String readLine(); 
String readUTF(); //读取字符串
void readFully(byte[] b);
void readFully(byte[] b, int off, int len);

void write(int b);
void writeUTF(String str);

//返回文件长度,返回值是long
long length();

//自定义读取,根据16进制字节码设置指针 偏移【量】,默认是以十六进制字节码的开头位置位参照
void seek(long pos); 

//从当前位置跳过字节
int skipBytes(int n); 

注意事项

写入和读取的数据类型的顺序必须一一对应,顺序不可以变化

//写入封装方法
public static void write() throws Exception{
        //1创建对象
        RandomAccessFile raf=new RandomAccessFile("ran.txt", "rw");
        //2写入
        raf.writeUTF("张三"); ---------------------------------------|
        raf.writeInt(20);	----------------------------------------|			
        raf.writeDouble(170.5);  									|
        raf.writeBoolean(true);									    |
    																|
   		raf.writeUTF("李四");										  |
        raf.writeInt(22);											|
        raf.writeDouble(180.5);										|
        raf.writeBoolean(false);                                    |
        															|
        //3关闭													   |
        raf.close();												|
        System.out.println("写入完毕");								  |
}																	|
//读取封装方法														|
public static void read() throws Exception{							|
	//1创建对象														  |
	RandomAccessFile raf=new RandomAccessFile("ran.txt", "r");      |
     																|   
    //2读取														   |
    String name = raf.readUTF(); ------------------------------------|
    int age = raf.readInt(); ----------------------------------------|
    double height = raf.readDouble();
    boolean b = raf.readBoolean();
    System.out.println(name+" "+age+" "+height+" "+b);

    //3关闭
    raf.close();
}

//该方式读取的还是张三的信息,如果需要读取李四的信息,则需要使用seek()或者skipBytes()方法

seek()和skipBytes()方法的执行过程和区别

执行原理分析

//同样以上面的代码为例,将写入的文件使用 EditPlus 打开,并使用Hx十六进制查看文件信息,如下:
00 06 E5 BC A0 E4 B8 89  00 00 00 14 40 65 50 00
00 00 00 00 01 00 06 E6  9D 8E E5 9B 9B 00 00 00
16 40 66 90 00 00 00 00  00 00 
/*
* 【00 06】表示的是占位6个字节的字符串,说明其后的6个位置【E5 BC A0 E4 B8 89】表示“张三”
*  Int占4个字节,所以【00 00 00 14】表示的是年龄age,14的十六进制转十进制就是20
*  同理,double类型占位8个字节,【40 65 50 00 00 00 00 00】表示170.5,最后的【01】表示true
*  综上所述,seek要定位在李四位置,即【00 06】的位置,相当于开头的【00】位置,偏移【量】为21
*/
raf.seek(21);
raf.skipBytes(21);

两种方法的区别

/*
* seek()方法,是指针的偏移量,始终以开头位置为参照,所以如下的代码不会报错
*/
raf.seek(21);
raf.seek(21);

/**
* skipBytes()方法是跳过,始终以前一个位置为参照,所以会出现EOF Exception,读取到文件末尾的异常
*/
raf.skipBytes(21);
raf.skipBytes(21);

使用 readFully() 方法完成复制操作案例

public class TextDemo01 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile file = new RandomAccessFile("file.txt", "rw");
		file.writeUTF("这是一个UTF字符串");
		file.writeBoolean(true);// 占1个字节
		file.writeShort(395);// 占2个字节
		file.writeLong(2325451l);// 占8个字节

		System.out.println("——————文件复制(从file到fileCopy)——————");
		file.seek(0);//从开头位置读取
		RandomAccessFile fileCopy = new RandomAccessFile("fileCopy.txt", "rw");
		int len = (int) file.length();// 取得文件长度,返回值是long,注意转型
		byte[] b = new byte[len];
		
        //全部读取
		file.readFully(b);
		fileCopy.write(b);
		System.out.println("复制完成!");
	}
}

标签:基本,raf,00,读取,int,RandomAccessFile,file,使用
来源: https://blog.csdn.net/qq_45337431/article/details/99202015