zoukankan      html  css  js  c++  java
  • RandomAccessFile类

    该类不是io体系中的子类(但在IO包中),是Object下的一个子类。随机访问文件,自身具备读写的方法,通过skipBytes(int x),seek(int x)这两个方法随机访问

    特点:
       1,该对象即能读,又能写。
       2,该对象内部维护了一个byte数组,并通过指针可以操作数组中的元素,
       3,可以通过getFilePointer方法获取指针的位置,和通过seek方法设置指针的位置。
       4,其实该对象就是将字节输入流和输出流进行了封装。
       5,该对象的源或者目的只能是文件。通过构造函数就可以看出。

    使用RandomAccessFile对象写入一些人员信息,比如姓名和年龄。该类中的写方法能将数据的原字节数写进去

    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            writeFile();
        }
        public static void writeFile() throws IOException{
             
            RandomAccessFile raf = new RandomAccessFile("F:\IO\ranacc.txt","rw");//如果文件不存在,则创建,如果文件存在,不创建
            
            raf.write("张三".getBytes());
            raf.writeInt(97);
            raf.write("小强".getBytes());
            raf.writeInt(99);
            
            raf.close();
        }
    }

    随机读:是通过seek()方法设置角标的位置,从这只的角标位置开始读取

     1 import java.io.IOException;
     2 import java.io.RandomAccessFile;
     3 
     4 public class Test {
     5     public static void main(String[] args) throws IOException {
     6         readFile();
     7     }
     8     public static void readFile() throws IOException {
     9         
    10         RandomAccessFile raf = new RandomAccessFile("F:\IO\ranacc.txt", "r");
    11         
    12         //通过seek设置指针的位置。
    13         raf.seek(0);//随机的读取。只要指定指针的位置即可。 
    14         
    15         byte[] buf = new byte[4];
    16         raf.read(buf);
    17         
    18         String name = new String(buf);
    19         
    20         int age = raf.readInt();
    21         
    22         System.out.println("name="+name);
    23         System.out.println("age="+age);
    24         
    25         System.out.println("pos:"+raf.getFilePointer());
    26         
    27         raf.close();
    28     }
    29 }

    随机写:是通过seek()方法设置角标的位置,从这只的角标位置开始写入

    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            randomWrite();
        }
        public static void randomWrite() throws IOException{
            //每次创建该对象,都新创建一个新数组,没有设置角标的位置默认是0角标开始
            RandomAccessFile raf = new RandomAccessFile("F:\IO\ranacc.txt", "rw");
            
            //往指定位置写入数据。
            raf.seek(100*8);
            
            raf.write("哈哈".getBytes());
            raf.writeInt(108);
            
            raf.close();
        }
    }

    往文件中写入三个员工的信息,然后随机顺序读取员工的信息

    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class Test {
        public static void main(String[] args) throws IOException {
           Employee e1 = new Employee("zhangsan", 23);
           Employee e2 = new Employee("Lisi", 27);
           Employee e3 = new Employee("Wangwu", 30);
           
           RandomAccessFile ra = new RandomAccessFile("F:\IO\employee.txt", "rw");
           
           ra.write(e1.name.getBytes());
           ra.writeInt(e1.age);
           ra.write(e2.name.getBytes());
           ra.writeInt(e2.age);
           ra.write(e3.name.getBytes());
           ra.writeInt(e3.age);
           
           ra.close();
           
           
           RandomAccessFile raf = new RandomAccessFile("F:\IO\employee.txt", "r");
           
           raf.skipBytes(12);//跳过第一个员工的信息,其中姓名8个字节,年龄4个字节
           System.out.println("第二个员工的信息是:");
           String str = "";
           for(int i = 0;i<Employee.LEN;i++){
               str = str+(char)raf.readByte();
           }
           System.out.println("name="+str.trim());
           System.out.println("age="+raf.readInt());
           
           
           System.out.println("第一个员工的信息是:");
           raf.seek(0);
           str = "";
           for(int i = 0;i<Employee.LEN;i++)
               str = str+(char)raf.readByte();
           System.out.println("name="+str.trim());
           System.out.println("age="+raf.readInt());
           
           
           
           raf.skipBytes(12);//跳过第二个员工的信息,其中姓名8个字节,年龄4个字节
           System.out.println("第三个员工的信息是:");
           str = "";
           for(int i = 0;i<Employee.LEN;i++){
               str = str+(char)raf.readByte();
           }
           System.out.println("name="+str.trim());//将一些姓名不够八个字节的员工,会有空格,可以使用字符串的trim方法将空格切掉
           System.out.println("age="+raf.readInt());
        }
        
    }
    class Employee{
        String name;
        int age;
        final static int LEN = 8;
        
        public Employee(String name,int age){
            if(name.length()>LEN){
                name = name.substring(0,8);
            }else{
                while(name.length()<LEN){
                    name +="u0000";
                }
            }
            this.name = name;
            this.age = age;
        }
    }
  • 相关阅读:
    Android监听ScrollView滑动到顶端和底部
    Error generating final archive: Unable to get debug signature key
    Android Service 通知Activity更新界面的方法研究
    android Service 的简单使用(转)
    GPS坐标转换为百度地图坐标
    GPS坐标换算为百度坐标(转)
    Android 实现在Activity中操作刷新另外一个Activity数据列表
    android textview 显示一行,且超出自动截断,显示"..."
    Android中界面实现全屏显示的两种方式
    kill命令详解
  • 原文地址:https://www.cnblogs.com/LO-ME/p/3599532.html
Copyright © 2011-2022 走看看