----------------------
ASP.Net+Android+IOS开发、
.Net培训、期待与您交流! ----------------------
RandomAccessFile是Java输入/输出流体系中功能最丰富的文件内容访问类,它提供了众多的方法来访问文件内容,既可以读取文件内容,也可以向文件输出数据。与普通的输入输出流不同的是,RandomAccessFile支持“随机访问”方式,程序可以直接跳转到文件的任意地方来读写数据。
Random在英语中不仅有随机的意思,还有任意的意思,所以RandomAccess翻译成任意访问更为合适。即可以访问文件的任意位置。
RandomAccessFile包含两个构造器,它们实质内容相同:
RandomAccessFile(File file, String mode):创建从中读取和向其中写入(可选)的随机访问文件流,该文件由File参数指定。
RandomAccessFile(String fileName, String mode):创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。
构造器的mode参数指定其访问模式,该参数有四个值,两个较为常用:
"r":以只读方式打开指定文件。如果试图对该RandomAccessFile执行写入方法都会抛出异常。
"rw":以读取、写入方式打开指定文件。如果文件不存在,那么创建文件;如果文件存在,则不会覆盖文件。
RandomAccessFile包含了一个记录指针,用以标识当前读写处的位置,当程序创建一个RandomAccessFile对象时,该对象的文件记录指针位于文件头(也就是0处),当读/写了n个字节后,文件记录指针将会向后移动n个字节。RandomAccessFile可以自由移动记录指针,既可以向前移动,也可以向后移动。RandomAccessFile包含了三个方法来来操作文件记录指针:
long getFilePointer():返回文件记录指针的当前位置。
void seek(long pos):将文件记录指针定位到pos位置。
int skipBytes(int n):尝试跳过输入的
n
个字节以丢弃跳过的字节。该方法只可以向后移动指针,不可向前移动。
RandomAccessFile既可以读文件,也可以写。它包含了完全类似于InputStream的三个read方法并且用法相同;也包含了完全类似于OutputStream的三个write方法,并且用法相同。
注意:RandomAccessFile类直接继承Object类而非基础IO四大基类,但它是io包的成员,属于IO体系。
下面程序使用RandomAccessFile来访问指定文件的中间部分数据。
public class RandomAccessFileDemo {
public static void main(String[] args) {
RandomAccessFile raf = null;
try{
//以只读方式打开一个RandomAccessFile对象
raf = new RandomAccessFile("d:\\demo.txt", "r");
//获取对象文件指针的位置,初始位置是0
System.out.println("指针初始位置" + raf.getFilePointer());
//移动指针位置
raf.seek(300);
byte[] buf = new byte[1024];
int len = 0;
while((len = raf.read(buf)) != -1){
System.out.print(new String(buf, 0, len));
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(raf != null)
raf.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
下面程序在指定文件后面追加内容。为了追加内容,程序应该先将记录指针移动到文件最后然后再向文件输出内容。
public class AppendContent {
public static void main(String[] args) {
RandomAccessFile raf = null;
try{
//以读写方式打开文件
raf = new RandomAccessFile("d:\\demo.txt", "rw");
//将记录指针移动到文件最后
raf.seek(raf.length());
raf.write("追加的内容\r\n".getBytes());
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(raf != null)
raf.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
RandomAccessFile依然不能向文件的指定位置插入内容。如果直接将文件记录指针移动到中间某位置后开始输出,则新输出内容覆盖文件中原有的内容。如果需要向指定位置插入内容,程序需要先把插入点后面内容读入缓冲区,等把需要插入的数据写入文件后,再将缓冲区的内容追加到文件后面。
下面程序实现了对某文件在指定位置插入内容。
public class InsertContent {
public static void main(String[] args) {
try {
inset("d:\\demo.txt", 45, "插入的内容\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
//在指定位置插入内容
public static void inset(String fileName, long pos, String insertContent)
throws IOException{
RandomAccessFile raf = null;
//创建一个临时文件保存插入点后的数据
File tempFile = File.createTempFile("tmp", null);
FileOutputStream tmpOut = null;
FileInputStream tmpIn = null;
tempFile.deleteOnExit();
try{
raf = new RandomAccessFile(fileName, "rw");
tmpOut = new FileOutputStream(tempFile);
tmpIn = new FileInputStream(tempFile);
//找到插入点
raf.seek(pos);
//---------下面代码将插入点后的内容读入临时文件中保存-------
byte[] buf = new byte[1024];
int len = 0;
while((len = raf.read(buf)) != -1){
//将读取的数据写入临时文件
tmpOut.write(buf, 0, len);
}
//---------下面代码将指定内容插入到文件----------
//将指针重新定位到插入点
raf.seek(pos);
//追加要插入的内容
raf.write(insertContent.getBytes());
//将临时文件的内容追加到
while((len = tmpIn.read(buf)) != -1){
raf.write(buf, 0, len);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(raf != null)
raf.close();
}catch(IOException e){
e.printStackTrace();
}
try{
if(tmpIn != null)
tmpIn.close();
}catch(IOException e){
e.printStackTrace();
}
try{
if(tmpOut != null)
tmpOut.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
小结:
RandomAccessFile类
1)直接继承Object类,但属于IO体系
2)只能操作文件
3)既可以读取文件,又可以向文件写入数据
4)其mode模式为r时,不会创建新文件,只可以在原有文件上读取,如果文件不存在,则抛出异常。
mode模式为rw时,当文件不存在,会创建该文件;如果文件存在,则不会覆盖。
5)没有在文件中插入内容的方法。
6)skipBytes方法只可以向后操作,不可以向前。
seek方法既可以向前,又可以向后。
拓展:
多线程、断点网络下载工具就可以通过RandomAccessFile类来实现。下载工具在下载开始时都会建立两个文件:一个与被下载文件大小相同的空文件,一个记录文件指针的位置。下载工具用多条线程启动输入流来读取网络数据,并使用RandomAccessFile将从网络上读取的数据写入空文件中,每写一些数据后,记录指针的文件分别记下每个RandomAccessFile当前的文件指针位置——如果网络断开后,下次开始下载时,每个RandomAccessFile都根据记录文件指针的文件中记录的位置继续向下写数据。
----------------------
ASP.Net+Android+IOS开发、
.Net培训、期待与您交流! ----------------------详细请查看:
http://edu.csdn.net