当前位置 博文首页 > 静Yu的博客:随机读写流

    静Yu的博客:随机读写流

    作者:[db:作者] 时间:2021-09-06 13:19

    ? 前面学习的文件输入流和文件输出流都是单向的,流中的数据必须按顺序
    进行读写
    ? 在某些情况下,程序需要不按照顺序随机地访问磁盘文件中的内容
    ? Java提供了专门用来处理文件随机存取的类RandomAccessFile

    RadomAccessFile类

    RandomAccessFile
    ----用来随机读取文件,其功能更完善
    ----类直接隶属于Object类
    ----类创建的流的指向既可以作为源,
    也可以作为目的地
    

    RandomAccessFile类的构造方法

    ? public RandomAccessFile(String name, String mode)
    throws FileNotFoundException
    ? public RandomAccessFile(File file, String mode)
    throws FileNotFoundException
    第二个参数mode取值:
            “r”— 以只读方式打开文件
            “rw” —以读写方式打开文件
    

    RandomAccessFile类的成员方法

    ? public long getFilePointer() throws IOException
    返回当前文件指针
    ? public void seek(long pos) throws IOException
    把文件指针置于给出的位置pos
    ? public long length() throws IOException
    返回文件长度
    ? public int skipBytes(int n) throws IOException
    从当前位置开始跳过n个字节
    

    在这里插入图片描述
    在这里插入图片描述

    cs
    下一篇:没有了