Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问。使用这个类,可以跳转到文件的任意位置读写数据。程序可以在随机文件中插入数据,而不会破坏该文件的其他数据。此外,程序也可以更新或删除先前存储的数据,而不用重写整个文件。

RandomAccessFile类是Object类的直接子类,包含两个主要的构造方法用来创 建RandomAccessFile 的对象,如表 10-11 所示。

表 10-11 RandomAccessFile 类的构造方法

构造方法

功能描述

public RandomAccessFile(String name, String mode)

指定随机文件流对象所对应的文件名,以 mode 表示对文件的访问模式

public RandomAccessFile (File file, String mode)

以 file 指定随机文件流对象所对应的文件名,以 mode 表示访问模式

需要注意的是,mode 表示所创建的随机读写文件的操作状态,其取值包括:

  • r:表示以只读方式打开文件。
  • rw:表示以读写方式打开文件,使用该模式只用一个对象即可同时实现读写操作。

表 10-12 列出了 RandowAccessFile 类常用的方法及说明。

表 10-12 RandowAccessFile 的常用方法

方法

功能描述

long length()

返回文件长度

void seek(long pos)

移动文件位置指示器,pos 指定从文件开头的偏离字节数

int skipBytes(int n)

跳过 n 个字节,返回数为实际跳过的字节数

int read()

从文件中读取一个字节,字节的高 24 位为 0,若遇到文件结尾,返回-1

final byte readByte()

从文件中读取带符号的字节值

final char readChar()

从文件中读取一个 Unicode 字符

final void writeChar(inte c)

写入一个字符,两个字节

【例 10-12】模仿系统日志,将数据写入到文件尾部。

//********** ep10_12.java **********

import java.io.*;

class ep10_12{

    public static void main(String args[]) throws IOException{

        try{

            BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

            String s=in.readLine();

            RandomAccessFile myFile=new RandomAccessFile("ep10_12.log","rw");

            myFile.seek(myFile.length());  //移动到文件结尾

            myFile.writeBytes(s+"\n");  //写入数据

            myFile.close();

        }

        catch(IOException e){}

    }

}


程序运行后在目录中建立一个 ep10_12.log 的文件,每次运行时输入的内容都会在该文件内容的结尾处添加。

***********利用多线程来对文件内容进行添加***************

 

1 public class TestThreadWriteData {
 2 
 3     /**
 4      * @param args
 5      * @throws IOException
 6      */
 7     public static void main(String[] args) throws IOException {
 8 
 9         RandomAccessFile raf = new RandomAccessFile("e:\\raf.txt", "rw");
10         // 分配了1M的内存
11         raf.setLength(1024 * 1024);
12         raf.close();
13         String content1 = "我是要写入的第一句话";
14         String content2 = "我是要写入的第二句话";
15         String content3 = "我是要写入的第三局话";
16         String content4 = "我是要写入的第四句话";
17         int sum = 0;
18         new MyThread(content1.getBytes(), 0).start();
19         sum += content1.getBytes().length;
20         new MyThread(content2.getBytes(), sum).start();
21         sum += content2.getBytes().length;
22         new MyThread(content3.getBytes(), sum).start();
23         sum += content3.getBytes().length;
24         new MyThread(content4.getBytes(), sum).start();
25     }
26 
27     private static class MyThread extends Thread {
28 
29         private int length1;
30 
31         private byte[] contentByte1;
32 
33         public MyThread(byte[] contentByte, int length) {
34             this.length1 = length;
35             this.contentByte1 = contentByte;
36         }
37 
38         @Override
39         public void run() {
40             try {
41                 RandomAccessFile raf1 = new RandomAccessFile("e:\\raf.txt",
42                         "rw");
43                 raf1.seek(length1);
44                 raf1.write(contentByte1);
45                 raf1.close();
46             } catch (IOException e) {
47                 e.printStackTrace();
48             }
49 
50         }
51 
52     }
53 
54 }