Java 随机读取文件 RandomAccessFile 基于字节 byte

更多 Java IO & NIO方面的文章,请参见文集《Java IO & NIO》


  • 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;
    有一个游标或者索引 file pointer,来访问字节数组
  • getFilePointer() 得到当前 file pointer
  • seek(long pos) 修改当前 file pointer

RandomAccessFile 的使用:

public static void main(String[] args) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("r.txt", "rw");

    raf.writeUTF("你好");
    System.out.println(raf.getFilePointer());
    raf.seek(0);
    String s = raf.readUTF();
    System.out.println(s);
}

输出:

8
你好

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Socket通信 参考: 国内博客 维基百科 IBM Knowledge 国外技术分享的文章 Oracle官方文档...
    风骨依存阅读 811评论 0 2
  • 今天是2016年12月31号。马上快要217年了,时间永远跑在你我的前面,可能比较享受被别人追的滋味把。刚刚看完《...
    火星小喵阅读 366评论 0 0
  • 黎明,你不要来哦 我无脸见你 清晨里,爱情花香飘逸 我这里别来 我还没穿好裤子 我还在昏迷 我带着孤独和酒气 死在...
    明明大叔阅读 179评论 2 4
  • 虽然如今好的配色方案已经唾手可得,但想为数据可视化找到合适的配色方案,难度仍然不小。因信息图的独特属性,在保证色彩...
    打豆豆阅读 2,109评论 0 3
  • 自信力 自信不是凭空而来,而是经过培养和磨练得来 1自卑,自信,自大 种麦子的人,都知道,麦子的收获也是需要付出和...
    持枢君阅读 306评论 0 2