Java 实现文件随机读写-RandomAccessFile

使用JAVA IO体系中的RandomAccessFile类来完成的话,可以实现零内存追加。其实这就是支持任意位置读写类的强大之处。

在这之前,还是先啰嗦的介绍下RandomAccessFile这个类,RandomAccessFile是Java中输入,输出流体系中功能最丰富的文件内容访问类,它提供很多方法来操作文件,包括读写支持,与普通的IO流相比,它最大的特别之处就是支持任意访问的方式,程序可以直接跳到任意地方来读写数据。

如果我们只希望访问文件的部分内容,而不是把文件从头读到尾,使用RandomAccessFile将会带来更简洁的代码以及更好的性能。

下面来看下RandomAccessFile类中比较重要的2个方法,其他的和普通IO类似,在这里,就不详细说明了。

方法名作用
getFilePointer()返回文件记录指针的当前位置
seek(long pos)将文件记录指针定位到pos的位置

读取任意位置的数据
追加数据,
任意位置插入数据,


import org.junit.Test;

import java.io.*;

/*
    总结: RandomAccessFile的参数(path, mode)
        mode : r rw rws rwd
 */
/*
    注意点: mode只能是 r rw rws rwd这四种 rws (read write sync )
    rwd
 */
public class RandomAccessFileTest {
    @Test
    public void test1() {
        String path = "D:\\input\\hello.txt";
        int seekPointer = 2000;
        try {
            randomRed(path, seekPointer);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void test2(){
        String path = "D:\\input\\hello.txt";
        try {
            randomWrite(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3(){
        String path="D:\\input\\hello.txt";

        try {
            insert(path, 33, "\n 优秀...");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //功能三: 任意位置插入数据
    public static void insert(String filename, long points, String insertContent) throws IOException {
        File tmp=File.createTempFile("tmp", null);
        tmp.deleteOnExit(); //设置在JVM退出时删除
        RandomAccessFile raf = new RandomAccessFile(filename, "rw");
        //创建一个临时文件夹保存插入点后的数据
        FileOutputStream tmpOut= new FileOutputStream(tmp);
        FileInputStream tmpIn = new FileInputStream(tmp);
        raf.seek(points); //寻找到需要对位置
        //将插入点后的数据读入到临时文件夹
        byte[] buffer=new byte[1024];

        int hasread=0;
        while((hasread= raf.read(buffer))>0){
            tmpOut.write(buffer,0,hasread);
        }
        //插入需要指定添加的数据
        raf.seek(points);//返回原来的插入处
        //追加需要追加的内容
        raf.write(insertContent.getBytes());
        //最后追加临时文件中的内容
        while((hasread=tmpIn.read(buffer))>0){
            raf.write(buffer,0,hasread);
        }

    }

    //功能二:追加数据
    public static  void randomWrite(String path) throws IOException {
        //以读写的方式建立
        RandomAccessFile raf = new RandomAccessFile(path, "rw");
        //将记录指针移动到文件的最后
        raf.seek(raf.length());
        raf.write("追加...\r\n".getBytes());
    }


    //功能一:读取任意位置的数据
    public static void randomRed(String path, int pointe) throws IOException {
        RandomAccessFile raf = new RandomAccessFile(path, "r"); // r只读   rw-读写 rws rwd
        //获取RandomAccessFile对象文件指针的位置
        System.out.println("RandomAccessFile文件指针的初始位置:" + raf.getFilePointer());
        raf.seek(pointe);//移动文件指针位置
        byte[] buff = new byte[1024];
        int hasRead = 0;
        while ((hasRead = raf.read(buff)) > 0) {
            System.out.println(new String(buff, 0, hasRead));
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值