RandomAccessFile类

RandomAccessFile类是java输入/输出体系中功能最丰富的文件内容访问类,它提供了众多的方法来访问文件内容,它既可以读取文件内容,也可以像文件输出数据。与普通的输入、输出流不同的是,RandomAccessFile支持“随机访问”,程序可以直接跳到文件的任意地方进行读写文件。

RandomAcessFile两个构造函数①、RandomAccessFile(String name,String mode)  ②、RandomAccessFile(File file,String mode)。一个使用String参数指定文件名,一个使用File参数指定文件本身。除此之外,还需要一个mode参数:

RandomAccessFile既可以读文件也可以写,所以它既包含了完全类似于InputStream的三个read方法:read()\read(byte[] b)\read(byte[] b,int off,int len)。同时也包含了完全类似于OutputStream的三个write方法。RandomAccessFile包含了如下两个方法来操作文件记录指针:

①、long getFilePointer(): 返回文件记录指针的当前位置

②、void seek(long pos):将文件记录指针定位到pos位置。

下面程序使用RandomAccessFile来访问指定的中间数据

 1 public class RandomAccessFileTest {
 2 
 3     /**
 4      * @param args
 5      */
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         /*
 9          * 创建RandomAceessFile对象有两种方式
10          */
11         File file=new File("d:/config.xml");
12         RandomAccessFile raf=null;
13         try{
14             raf=new RandomAccessFile(file,"r");
15             System.out.println(raf.getFilePointer());
16             raf.seek(20);
17             System.out.println("现在指针的位置在哪里"+raf.getFilePointer());
18             String line=null;
19             while((line=raf.readLine())!=null){
20                 System.out.println(line);
21             }
22         }
23         catch(Exception e){
24             System.out.println(e.toString());
25         }
26         finally{
27             if(raf!=null)
28                 try
29                {
30                     raf.close();
31                }
32             catch(Exception e){
33                 System.out.println(e.toString());
34             }
35                 
36         }
37 
38     }
39 
40 }
View Code

下面程序使用RandomAccessFile来追加内容

 1 package com.edu.io;
 2 
 3 import java.io.File;
 4 import java.io.RandomAccessFile;
 5 
 6 public class RandomAccessFileTest {
 7 
 8     /**
 9      * @param args
10      */
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         /*
14          * 创建RandomAceessFile对象有两种方式
15          */
16         File file=new File("d:/config.xml");
17         RandomAccessFile raf=null;
18         try{
19             raf=new RandomAccessFile(file,"rw");
20             raf.seek(raf.length());
21             raf.write("新加入的内容".getBytes());
22         }
23         catch(Exception e){
24             System.out.println(e.toString());
25         }
26         finally{
27             if(raf!=null)
28                 try
29                {
30                     raf.close();
31                }
32             catch(Exception e){
33                 System.out.println(e.toString());
34             }
35                 
36         }
37 
38     }
39 
40 }
View Code

  多线程、断点的网络下载工具,就可通过RandomAccessFile类来实现,所有下载工具在下载开始时会创建两个文件:一个与被下载大小相同的空文件,一个记录文件指针的位置,下载工具用多条线程启动输入流来读取网络数据,并使用RandomAccessFile将从网络上读取的数据写入前面创建的空文件中,每写一些数据后,记录文件指针的文件则分别记下每个RandomAccessFile当前的文件指针的位置----------如果网络断开后,再次开始下载时,每个RandomAccessFile都根据记录文件指针的文件中记录的位置继续向下写数据。

 

posted on 2014-02-22 12:37  那些年我们一起奋斗的日子  阅读(852)  评论(0编辑  收藏  举报