Java学习笔记 (二十二) RandomAccessFile

RandomAccessFile

RandomAccessFile 该类的实例支持对文件的任意位置进行读写操作。 访问文件任意位置的行为类似于存储在文件系统中的大量字节数组。有一种游标,或指向隐含数组的索引,称为文件指针;输入操作从文件指针开始读取字节,并将文件指针向前推进,超过所读取的字节。如果文件是在读/写模式下创建的,那么输出操作也是可用的;输出操作从文件指针开始写入字节,并将文件指针向前推进,超过写入的字节。写p的输出操作

----------------------------官网原文----------------------------
     Instances of this class support both reading and writing to a random access file. 
  A random access file behaves like a large array of bytes stored in the file system.
 There is a kind of cursor, or index into the implied array, called the file pointer; 
 input operations read bytes starting at the file pointer and advance the file pointer 
 past the bytes read. If the random access file is created in read/write mode, 
 then output operations are also available; output operations write bytes starting 
 at the file pointer and advance the file pointer past the bytes written. Output
 operations that write past the current end of the implied array cause the array
 to be extended. The file pointer can be read by the getFilePointer method and
 set by the seek method.

实例化 RandomAccessFile

RandomAccessFile file=new RandomAccessFile("千字文.txt", "rw");

第一个参数是文件地址,
第二个参数model 是访问文件的模式,

model 参数详解

“r” : 仅仅只读,此模式下调用任何 "write "方法,都会抛出java.io.IOException 异常。
“rw” : 读和写 ,此模式下,如果文件还不存在,将会创建文件。
“rws” :读和写,和 “rw” 一样,并且还要求对文件内容或元数据的每次更新都要同步写入底层存储设备
“rwd” :读和写,和 “rw” 一样,并且还要求对文件内容的每次更新都要同步写入底层存储设备。

移动文件指针

移动文件指针到读写的位置,可以使用seek() 方法。
getFilePointer() 方法可以获取文件指针的当前位置。
如下示例:

        //实例化对象 读写模式
        RandomAccessFile file =new RandomAccessFile("千字文.txt", "rw");
        //移动文件指针到第 666个字节上
        file.seek(666);
        //获取当前文件指针的位置
        System.out.println("文件指针位置:"+file.getFilePointer());

输出如下:
在这里插入图片描述

读取文件内容

读取 千字文.txt ,从第34个字节开始读取,读取 68个字节。
文本内容如下:
在这里插入图片描述
代码如下:

    try(RandomAccessFile file=new RandomAccessFile("千字文.txt", "rw"))
        {
            byte[] data=new byte[68];
            //移动到 第34个字节
            file.seek(34);
             file.read(data);
            System.out.println(new String(data,"utf-8"));
        }

输出如下:
在这里插入图片描述
从上图可以看出,从第二行开始读取的,读取了两行内容,符合我们的预期。

第一次读取之后文件指针在哪呢?

代码如下:

 try(RandomAccessFile file=new RandomAccessFile("千字文.txt", "rw"))
        {
            byte[] data=new byte[68];
            file.seek(34);
            System.out.println("读取内容之前文件指针位置:"+file.getFilePointer());

            file.read(data);
            System.out.println(new String(data,"utf-8"));

            System.out.println("读取内容之后文件指针位置:"+file.getFilePointer());
        }

输出如下:
在这里插入图片描述
从上图可以看出,读取内容之后,文件指针也随之移动到了,读取的字节数组末尾的那个字节。

写入文件内容

写入文件内容与读取文件内容 ,操作指针方法是一样的,
写入之后 文件指针位置也会随之移动。
如果 创建实例的时候,模式为 “r” 将会抛出java.io.IOException 异常。
代码如下:

   try(RandomAccessFile file=new RandomAccessFile("千字文.txt", "rw"))
        {
            byte[] data=new byte[68];
            file.seek(34);
            file.write("I Love You".getBytes());
            System.out.println("写入内容的字节数:"+"I Love You".getBytes().length+" 写入内容之后文件指针位置:"+file.getFilePointer());
        }

输出如下:
在这里插入图片描述

推荐阅读

Java学习笔记 (二十一) 使用 try-with-resources语句关闭资源.

参考资料

1.Class RandomAccessFile
2.Java IO: RandomAccessFile

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值