RandomAccessFile——随机读取与写入流

2019-06-26  本文已影响0人  kanaSki

构造函数:RandomAccessFile(String name, String mode)
RandomAccessFile(File name, String mode)
mode取值为"r","rw","rws","rwd "
该类的实例支持读取和写入随机访问文件。
seek方法可以跳转到文件指定的字节处(设置文件指针偏移)

    public void readSequence(int beginIndex, int actualSize) throws IOException {
        RandomAccessFile raf = new RandomAccessFile("pom.xml", "r");
        raf.seek(beginIndex);
        byte[] bytes = new byte[1024];
        int len = -1;
        while ((len = raf.read(bytes)) != -1) {
            if (len < actualSize) {
                System.out.println(new String(bytes, 0, len));
                actualSize -= 1024;
            } else {
                System.out.println(new String(bytes, 0, actualSize));
            }
        }
        raf.close();
    }
上一篇 下一篇

猜你喜欢

热点阅读