zoukankan      html  css  js  c++  java
  • IO--RandomAccessFile类

    RandomAccessFile类可以实现对文件的随机读写操作。

    1. 新建RandomAccessFile对象的文件位置指针位于文件的开头处;
    2. 每次读写操作之后,文件位置指针都相应后移读写的字节数;
    3. 利用getPointer()方法可获取当前文件位置指针从文件头算起的绝对位置;
    4. 利用seek()方法可以移动文件位置指针(seek(long pos)方法将文件位置指针移动到参数pos指定的从文件头算起的绝对位置处);
    5. length()方法将返回文件的字节长度(根据文件长度和位置指针相比较,可以判断是否读到了文件尾)。
    在文件中指定位置插入字符:

    public static void main(String[] args) throws Exception {
    RandomAccessFile raf = new RandomAccessFile(new File("test.txt"), "rw");
    raf.seek(7);
    StringBuffer sb = new StringBuffer();
    byte[] b = new byte[20];
    int len;
    /*将指针后内容复制到sb中*/
    while((len = raf.read(b)) != -1){
    String str = new String(b,0,len);
    sb.append(str);
    }
    raf.seek(7);//移动指针到指定位置
    raf.write("xyz".getBytes());//写入要插入的字符
    raf.write(sb.toString().getBytes());

    raf.close();
    }
  • 相关阅读:
    记ArcGIS Android V100.4加载天地图不显示的问题
    arcgis性能检测记录
    python数据更新
    python批量definition query
    从天地图下载瓦片构建本地瓦片地图
    android studio问题备注
    android studio报butterknife错误
    openPose-注
    视觉工程师现场调试手册
    human pose estimation
  • 原文地址:https://www.cnblogs.com/wjunxia/p/7082827.html
Copyright © 2011-2022 走看看