欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java使用RandomAccessFile类基于指针读写文件实例代码

程序员文章站 2024-02-19 12:02:28
java api中提供了一个基于指针操作实现对文件随机访问操作的类,该类就是randomaccessfile类,该类不同于其他很多基于流方式读写文件的类。它直接继承自obj...

java api中提供了一个基于指针操作实现对文件随机访问操作的类,该类就是randomaccessfile类,该类不同于其他很多基于流方式读写文件的类。它直接继承自object。

public class randomaccessfile extends objectimplements dataoutput, datainput, closeable{...}

1.使用该类时可以指定对要操作文件的读写模式。

第一种模式是只读模式,第二种模式是读写模式。在创建该类实例时指定。

@test 
  public void test01() throws ioexception{ 
    //读写模式 
    randomaccessfile r=new randomaccessfile(new file(""),"rw" ); 
	//只读模式 
	//<span style="font-family:arial, helvetica, sans-serif;">randomaccessfile r=new randomaccessfile(new file(""),"r" );</span> 
    r.write(1);//写出一个字节,写出的是int值的低8位 
    r.close(); 
    r.read();//每次读一个字节,填充到int的低八位 
  } 

2.字节读取操作

(1)void write(int d):写出该int值的低8位,其他位舍弃

@test 
  public void testwrite() throws ioexception{ 
    randomaccessfile file=new randomaccessfile(new file("emp.txt"),"rw"); 
    file.write(97);//0110 0001 
    file.write(98);//0110 0010 
    file.write(99);//0110 0011 
  } 

(2)int read():注意:该方法只会从文件中当前指针处读取一个byte(8位)的数据填充到int返回值的第八位,其他位用0填充。若该方法返回0,则代表读到文件末尾。

以上两种方法使用起来当然会很麻烦。

randomaccessfile类中还封装了对8中基本数据类型的读写操作,使用起来会很方便。

分别为

readbyte(), readshort(), readint(), readlong(), readfloat(),readdouble(),readboolean(),readchar()

返回值类型均为对应的基本数据类型。同时相应的也有这八中writebyte()...方法。这是在流式读写文件中所不具有的。

3.文件指针操作

randomaccessfile类的所有读写操作均是基于指针的,指针会随着读或写的操作往后移动。同时也可以通过方法*的操作指针的位置,以此更方便的读写文件。

常用方法

(1)long getfilepointer():该方法用于返回当前指针位置。默认读写操作时,指针的初始位置为文件的第一个字节处,即值为0
(2)void seek(long pos):该方法可以设定指针位置
(3)int skipbytes(int n):该方法可以跳过n个字节

/** 
 * randomaccessfile:基于指针读写,总是在指针当前位置读写,无论读写,指针都会向后移动 
 * randomaccessfile总是在指针当前位置进行读写字节,并且无论进行了读还是写一个字节后, 
 * 指针都会自动向后移动到下一个字节的位置 
 * 默认创建出来randomaccessfile时,指针位置为0,即:文件的第一个字节的位置 
 * @author zc 
 */ 
public class t13randomaccessfile { 
  public static void main(string[] args) throws ioexception { 
    randomaccessfile raf=new randomaccessfile(new file("emp.txt"),"rw"); 
    int a=255; 
    //虽然能写入,但是在记事本中打开仍是乱码字符 
    raf.write(a>>>24);//11111111 
    raf.write(a>>>16); 
    raf.write(a>>>8); 
    raf.write(a); 
    //获取当前指针位置 
    long pos=raf.getfilepointer(); 
    //!!注意,返回值是字节4,前面写入的四个字节对应从0--3字节 
    system.out.println("pos:"+pos);//4 
    //将int值分成四部分,写入 
    raf.writeint(255); 
    system.out.println("pos:"+raf.getfilepointer());//8 
    raf.writelong(255);//long八个字节 
    system.out.println("pos:"+raf.getfilepointer());//16 
    raf.writefloat(8);//float四个字节 
    system.out.println("pos:"+raf.getfilepointer());//20 
    raf.writedouble(12.1);//double八个字节 
    system.out.println("pos:"+raf.getfilepointer());//28 
    raf.write(new byte[]{1,1}); 
    raf.writeboolean(false); 
    raf.writechar(1); 
    //此时已经写完,指针指向文件某位 
    system.out.println(raf.read());//-1 
    /* 
     *void seek(long pos) 
     *将指针移动到指定位置 
     * */ 
    raf.seek(0); 
    system.out.println("point:"+raf.getfilepointer());//0 
    //读取刚刚写入的long值 
    raf.seek(8); 
    long l=raf.readlong(); 
    system.out.println("读出的long值:"+l);//255 
    //读取刚刚写入的double值 
    raf.seek(20); 
    system.out.println(raf.readdouble());//12.1 
    raf.close(); 
  } 
} 

总结

以上就是本文关于java使用randomaccessfile类基于指针读写文件实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:java中的静态内部类详解及代码示例java源码解析之object类等,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!