A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© libin 中级黑马   /  2015-7-23 09:09  /  305 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1.该类不是IO体系中的子类,而是直接继承Object。但是它是IO包中的成员,因为它具备读和写功能。内部封装了一个数组,而且通过指针对数组的元素进行操作。可以通过getFilePointer获取指针位置。同时可以通过seek改变指针位置。其实完成读写操作的原理就是内部封装了字节输入流和输出流。

2.RandomAccessFile(File file,String mode)  RandomAccessFile(String name,String mode)  两个构造函数可以看出该类只能操作文件,而且操作文件还有模式。Mode参数用以打开文件的访问方式,其值为:
r  只读,不会创建文件,会去读取一个已存在的文件,如果文件不存在,则会出现异常。
rw 读写  文件不存在,会自动创建,如果存在不会覆盖。
rws  读写,还要求对文件的内容或元数据的每个同步更新都同步写入到底层存储设备。
rwd  读写,还要求对文件的内容的每个更新都同步写入到底层存储设备。

3.RandomAccessFile类的重要方法:
①达到随机读取数据的两种方法:
void        seek(long pos)   设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
int        skipBytes(int n)  尝试跳过输入的 n 个字节以丢弃跳过的字节。
②读操作,不同数据类型用不同的read方法
int        read()      从此文件中读取一个数据字节。
int        read(byte[] b)  将最多 b.length 个数据字节从此文件读入 byte 数组。
int        read(byte[] b, int off, int len)   将最多 len 个数据字节从此文件读入 byte 数组。
boolean        readBoolean()   从此文件读取一个 boolean。
byte        readByte()   从此文件读取一个有符号的八位值。
char        readChar()   从此文件读取一个字符。
double        readDouble()   从此文件读取一个 double。
float        readFloat()    从此文件读取一个 float。
void        readFully(byte[] b)
将 b.length 个字节从此文件读入 byte 数组,并从当前文件指针开始。
void        readFully(byte[] b, int off, int len)
将正好 len 个字节从此文件读入 byte 数组,并从当前文件指针开始。
int        readInt()   从此文件读取一个有符号的 32 位整数。
String        readLine()   从此文件读取文本的下一行。
long        readLong()    从此文件读取一个有符号的 64 位整数。
short        readShort()   从此文件读取一个有符号的 16 位数。
int        readUnsignedByte()  从此文件读取一个无符号的八位数。
int        readUnsignedShort()   从此文件读取一个无符号的 16 位数。
String        readUTF()   从此文件读取一个字符串。
③写操作,为了避免数据丢失,不同数据类型写入用不同的write方法
void        write(byte[] b)
将 b.length 个字节从指定 byte 数组写入到此文件,并从当前文件指针开始。
void        write(byte[] b, int off, int len)
将 len 个字节从指定 byte 数组写入到此文件,并从偏移量 off 处开始。
void        write(int b)
向此文件写入指定的字节。
void        writeBoolean(boolean v)
按单字节值将 boolean 写入该文件。
void        writeByte(int v)
按单字节值将 byte 写入该文件。
void        writeBytes(String s)
按字节序列将该字符串写入该文件。
void        writeChar(int v)
按双字节值将 char 写入该文件,先写高字节。
void        writeChars(String s)
按字符序列将一个字符串写入该文件。
void        writeDouble(double v)
使用 Double 类中的 doubleToLongBits 方法将双精度参数转换为一个 long,然后按八字节数量将该 long 值写入该文件,先定高字节。
void        writeFloat(float v)
使用 Float 类中的 floatToIntBits 方法将浮点参数转换为一个 int,然后按四字节数量将该 int 值写入该文件,先写高字节。
void        writeInt(int v)
按四个字节将 int 写入该文件,先写高字节。
void        writeLong(long v)
按八个字节将 long 写入该文件,先写高字节。
void        writeShort(int v)
按两个字节将 short 写入该文件,先写高字节。
void        writeUTF(String str)
使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件。
④获取和设置文件长度
long        length()    返回此文件的长度。
void        setLength(long newLength)  设置此文件的长度。

4.练习
  1. import java.io.*;
  2. class Test46 //RamdomAccessFile的练习
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 writeFile();
  7.                 readFile_1();
  8.                                    readFile_2();
  9.         }
  10.         public static void writeFile()  //写操作
  11.         {
  12.                 RandomAccessFile raf=null;
  13.                 try
  14.                 {
  15.                         raf=new RandomAccessFile("random.txt","rw");
  16.             raf.write("Lily".getBytes());   //不要忘了变成字节
  17.                     raf.writeInt(12);
  18.             raf.write("Jack".getBytes());
  19.                     raf.writeInt(13);
  20.                 }
  21.                 catch (IOException e)
  22.                 {
  23.                         System.out.println("写入错误");
  24.                 }
  25.                 finally
  26.                 {
  27.            try
  28.            {
  29.                            if(raf!=null)
  30.                          raf.close();
  31.            }
  32.            catch (IOException e)
  33.            {
  34.                            System.out.println("关闭流发生错误");
  35.            }
  36.                 }       
  37.         }
  38.         public static void readFile_1()  //读操作
  39.         {
  40.                 RandomAccessFile raf=null;  
  41.        try
  42.        {
  43.                   raf=new RandomAccessFile("random.txt","r");   //只读,mode选择r就行
  44.           byte[] b=new byte[4];                         //一个字母一字节,存入的名字都是4字母,字节数组长度为4,刚好存入一个名字
  45.                   raf.read(b);
  46.                   System.out.println("name="+new String(b));    //注意不同类型数据读取方式不同
  47.                   int age;
  48.                   age = raf.readInt();
  49.               System.out.println("age="+age);
  50.        }
  51.        catch (IOException e)
  52.        {
  53.                    System.out.println("读取文件发生错误");
  54.        }
  55.            finally
  56.           {
  57.                    try
  58.                    {
  59.                          if ( raf!=null)
  60.                           raf.close();
  61.                    }
  62.                    catch (IOException e)
  63.            {
  64.                      System.out.println("未能关闭");
  65.            }
  66.           }
  67.                
  68.         }

  69. public static void readFile_2()  //随机读操作
  70.         {
  71.                 RandomAccessFile raf=null;  
  72.        try
  73.        {
  74.                   raf=new RandomAccessFile("random.txt","r");  
  75.                   raf.seek(8);                              //指针指到下一个名字的地方,读取的是Jack  13
  76.           byte[] b=new byte[4];                      //或者插入raf.skipByes(8),能达到同样效果   
  77.                   raf.read(b);
  78.                   System.out.println("name="+new String(b));
  79.                   int age;
  80.                   age = raf.readInt();
  81.               System.out.println("age="+age);
  82.        }
  83.        catch (IOException e)
  84.        {
  85.                    System.out.println("读取文件发生错误");
  86.        }
  87.            finally
  88.           {
  89.                    try
  90.                    {
  91.                          if ( raf!=null)
  92.                           raf.close();
  93.                    }
  94.                    catch (IOException e)
  95.            {
  96.                      System.out.println("未能关闭");
  97.           }
  98.           }
  99.                
  100.         }
  101. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马