• 吴裕雄--天生自然JAVAIO操作学习笔记:RandomAccessFile


    import java.io.File ;
    import java.io.RandomAccessFile ;
    public class RandomAccessFileDemo01{
        // 所有的异常直接抛出,程序中不再进行处理
        public static void main(String args[]) throws Exception{
            File f = new File("d:" + File.separator + "test.txt") ;    // 指定要操作的文件
            RandomAccessFile rdf = null ;        // 声明RandomAccessFile类的对象
            rdf = new RandomAccessFile(f,"rw") ;// 读写模式,如果文件不存在,会自动创建
            String name = null ;
            int age = 0 ;
            name = "zhangsan" ;            // 字符串长度为8
            age = 30 ;                    // 数字的长度为4
            rdf.writeBytes(name) ;        // 将姓名写入文件之中
            rdf.writeInt(age) ;            // 将年龄写入文件之中
            name = "lisi    " ;            // 字符串长度为8
            age = 31 ;                    // 数字的长度为4
            rdf.writeBytes(name) ;        // 将姓名写入文件之中
            rdf.writeInt(age) ;            // 将年龄写入文件之中
            name = "wangwu  " ;            // 字符串长度为8
            age = 32 ;                    // 数字的长度为4
            rdf.writeBytes(name) ;        // 将姓名写入文件之中
            rdf.writeInt(age) ;            // 将年龄写入文件之中
            rdf.close() ;                // 关闭
        }
    };
    import java.io.File ;
    import java.io.RandomAccessFile ;
    public class RandomAccessFileDemo02{
        // 所有的异常直接抛出,程序中不再进行处理
        public static void main(String args[]) throws Exception{
            File f = new File("d:" + File.separator + "test.txt") ;    // 指定要操作的文件
            RandomAccessFile rdf = null ;        // 声明RandomAccessFile类的对象
            rdf = new RandomAccessFile(f,"r") ;// 以只读的方式打开文件
            String name = null ;
            int age = 0 ;
            byte b[] = new byte[8] ;    // 开辟byte数组
            // 读取第二个人的信息,意味着要空出第一个人的信息
            rdf.skipBytes(12) ;        // 跳过第一个人的信息
            for(int i=0;i<b.length;i++){
                b[i] = rdf.readByte() ;    // 读取一个字节
            }
            name = new String(b) ;    // 将读取出来的byte数组变为字符串
            age = rdf.readInt() ;    // 读取数字
            System.out.println("第二个人的信息 --> 姓名:" + name + ";年龄:" + age) ;
            // 读取第一个人的信息
            rdf.seek(0) ;    // 指针回到文件的开头
            for(int i=0;i<b.length;i++){
                b[i] = rdf.readByte() ;    // 读取一个字节
            }
            name = new String(b) ;    // 将读取出来的byte数组变为字符串
            age = rdf.readInt() ;    // 读取数字
            System.out.println("第一个人的信息 --> 姓名:" + name + ";年龄:" + age) ;
            rdf.skipBytes(12) ;    // 空出第二个人的信息
            for(int i=0;i<b.length;i++){
                b[i] = rdf.readByte() ;    // 读取一个字节
            }
            name = new String(b) ;    // 将读取出来的byte数组变为字符串
            age = rdf.readInt() ;    // 读取数字
            System.out.println("第三个人的信息 --> 姓名:" + name + ";年龄:" + age) ;
            rdf.close() ;                // 关闭
        }
    };
  • 相关阅读:
    python 正则表达式
    python -- 程序异常与调试(程序调试)
    python -- 程序异常与调试(异常处理)
    python -- 程序异常与调试(识别异常)
    python -- 日期与时间
    python -- 模块与类库
    python -- 面向对象编程(继承、重写)
    python -- 面向对象编程(属性、方法)
    python -- 面向对象编程(类、对象)
    python -- 函数
  • 原文地址:https://www.cnblogs.com/tszr/p/12161197.html
Copyright © 2020-2023  润新知