Java IO-RandomAccessFile

来源:互联网 发布:华为麦芒6怎么样知乎 编辑:程序博客网 时间:2024/05/16 17:01
 RandomAccessFile主要功能是随机读取,可以读取指定位置的内容,可以跳过指定的字节数读取
Java代码 复制代码 收藏代码
  1. package com.zzh.io;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5. import java.io.RandomAccessFile;   
  6.   
  7. public class RandomAccessFileDemo {   
  8.        
  9.     /**  
  10.      * RandomAccessFile主要功能是随机读取,可以读取指定位置的内容,可以跳过指定的字节数读取 
  11.      */  
  12.     public static void main(String[] args) throws IOException {        
  13.         testWrite();   
  14.         testRead();   
  15.     }   
  16.   
  17.     private static void testWrite() throws IOException {   
  18.         File file = new  File("D:" + File.separator + "test.txt");   
  19.         //RandomAccessFile的构造函数的第1个参数表示文件,  
  20.         //第2个参数表示文件打开的模式,常用有3种:r,w,rw   
  21.         //rw:如果文件不存在,会自动创建   
  22.         RandomAccessFile raf = new RandomAccessFile(file, "rw");   
  23.            
  24.         String name = null;   
  25.         int age = 0;   
  26.            
  27.         name = "zhangsan";   
  28.         age = 18;   
  29.         raf.writeBytes(name);   
  30.         raf.writeInt(age);   
  31.            
  32.         name = "lisi    ";//这里要补全空格,使字符串为8位,否则读取时会出错  
  33.         age = 19;   
  34.         raf.writeBytes(name);   
  35.         raf.writeInt(age);   
  36.            
  37.         name = "wangwu  ";//这里要补全空格,使字符串为8位,否则读取时会出错  
  38.         age = 20;   
  39.         raf.writeBytes(name);   
  40.         raf.writeInt(age);   
  41.            
  42.         raf.close();//关闭资源文件,切记!   
  43.     }   
  44.   
  45.     private static void testRead() throws IOException {   
  46.         File file = new  File("D:" + File.separator + "test.txt");   
  47.         RandomAccessFile raf = new RandomAccessFile(file, "r");   
  48.            
  49.         String name = null;   
  50.         int age = 0;           
  51.         byte[] b = new byte[8];   
  52.            
  53.         raf.skipBytes(12);//跳过第一个人的12个字节,读第二个人的信息  
  54.         for (int i = 0; i < b.length; i++) {   
  55.             b[i] = raf.readByte();//读取一个字节  
  56.         }   
  57.         name = new String(b);   
  58.         age = raf.readInt();   
  59.         System.out.println("第二个人的姓名:" + name + ",   年龄:" + age);   
  60.   
  61.         raf.seek(0);//将指针定位回文件开始处,读第一个人的信息  
  62.         for (int i = 0; i < b.length; i++) {   
  63.             b[i] = raf.readByte();//读取一个字节  
  64.         }   
  65.         name = new String(b);   
  66.         age = raf.readInt();   
  67.         System.out.println("第一个人的姓名:" + name + ",   年龄:" + age);   
  68.            
  69.         raf.seek(24);//将指针定位到第三个人处,读第三个人的信息  
  70. //      raf.skipBytes(12);//空出第二个人的信息,读第三个人的信息  
  71.         for (int i = 0; i < b.length; i++) {   
  72.             b[i] = raf.readByte();//读取一个字节  
  73.         }   
  74.         name = new String(b);   
  75.         age = raf.readInt();   
  76.         System.out.println("第三个人的姓名:" + name + ",   年龄:" + age);   
  77.            
  78.         raf.close();//关闭资源文件,切记!   
  79.     }   
  80.        
  81. }  
原创粉丝点击