zoukankan      html  css  js  c++  java
  • RandomAccessFile类

    构造器: public  RandomAccessFile(File  file, String  mode)

                   public  RandomAccessFile(String name, String  mode)

     创建RandomAccessFile类实例需要指定一个mode参数,

                   该参数指定RandomAccessFile的访问模式:

                                         r:以只读方式打开

                                      rw: 读取和写入

                                    rwd:读取写入 同步更新文件内容

                                    rws:读取和写入 同步文件内容和元数据的更新

    读取文件内容:

    RandomAccessFile raf = new RandomAccessFile(“test.txt”, “rw”);     
        raf.seek(5);
        byte [] b = new byte[1024];
    int off = 0; int len = 5; raf.read(b, off, len); String str = new String(b, 0, len); System.out.println(str); raf.close();

    写入文件内容:

    RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
        raf.seek(5);
            
        //先读出来
        String temp = raf.readLine();
            
        raf.seek(5);
        raf.write("xyz".getBytes());
        raf.write(temp.getBytes());
            
        raf.close();

       

    All that work will definitely pay off
  • 相关阅读:
    华为软件开发云评测
    个人技术博客(α)
    结对第二次作业
    结对第一次作业----部门通
    数独设计
    2017软件工程实践第一次作业
    Alpha冲刺博客集
    个人作业——软件工程实践总结作业
    用户使用调查报告
    总结随笔(Beta)
  • 原文地址:https://www.cnblogs.com/afangfang/p/12610074.html
Copyright © 2011-2022 走看看