一、前言
文件的读取或存储信息主要有两种方式:
RandomAccessFile
是一个完全独立的类, RandomAccessFile
虽然属于java.io包,但其拥有和我们认识的其它的IO类型有本质不同的行为,可以在一个文件内向前和向后移动。 二、作用说明
2.1、
作用
:RandomAccessFile 同时支持文件的读和写,并且它支持随机访问。是通过文件指针实现的 看一下其主要方法
void write(int d) //向文件中写入1个字节,写入的是传入的int值对应二进制的低8位;
void write(int b) //往文件中写入1个字节的内容,所写的内容为传入的int值对应二进制的低8位;
write(byte b[]) //往文件中写入一个字节数组的内容;
write(byte b[], int off, int len) //往文件中写入从数组b的下标off开始len个字节的内容;
int read() //读取1个字节,并以int形式返回,如果返回-1则代表已到文件末尾;
int read(byte[] data) /*一次性从文件中读取字节数组总长度的字节量,并存入到该字节数组中,
返回的int值代表读入的总字节数,如果返回-1则代表未读取到任何数据。
通常字节数组的长度可以指定为1024*10(大概10Kb的样子,效率比较好);*/
int read(byte[] data, int off, int len) /*一次性从文件中读取最多len个字节,
并存入到data数组中,从下标off处开始;*/
seek (long pos) //设置文件指针偏移量为指定值,即在文件内移动至新的位置;
long getFilePointer() //获取文件指针的当前位置;
void close() //关闭RandomAccessFile;
2.2、
访问模式
说明: "rw", "rws", "rwd"
的区别
当操作的文件是存储在本地的 基础存储设备 上时(如硬盘, NandFlash等),"rws" 或 "rwd", "rw" 才有区别。当操作的是基础存储设备上的文件时:如下
rws:
每次“更改文件内容[如 write()写入数据
]” 或 “ 修改文件元数据
(如文件的mtime)”时,都会将这些改变 同步
到基础存储设备上。 rwd:
每次“更改文件内容[如 write()写入数据
]”时,都会将这些改变 同步
到基础存储设备上。 rw:
关闭文件时
,会将“文件内容的修改” 同步
到基础存储设备上。至于,“更改文件内容”时,是否会立即同步, 取决于
系统底层实现。
2.3、
demo
2.3.2、构造函数: RandomAccessFile(File file/String filename, "model");
//demo01:_
File file = new File("data.txt");
try (RandomAccessFile raf = new RandomAccessFile(file, "rw");){
raf.write(1000); //这里写入一个字节(1000对应二进制的低八位)
raf.seek(0); //跳到文件头部
System.Out.println(raf.read());//输出:232
raf.seek(0); //跳到文件头部
System.out.println(raf.readInt());//试图读取一个整数,字节数不够
}catch (IOException e){
e.printStackTrace();
}
//demo02:_向文件中间追加内容。记住是中间
try(File file = new File("data2.txt");
RandomAccessFile raf = new RadnomAccessFile(file, "rw");
// 创建临时空文件用于缓冲,并指定在虚拟机停止时将其删除
File temp = File.createTempFile("temp", null);){
temp.deleteOnExit();
raf.seek(0);
raf.write("烟村四五家".getBytes());
raf.seek(0);
long dataLen = "烟村四五家".length(); // 获取文件当前长度
byte[] data1 = new byte[dataLen+2];
raf.read(data1, 0, dataLen);
System.out.println(new String(data1));
//把插入点后的内容保存起来
raf.seek(dataLen); // 将位置定位到插入点之后
FileInputStream fis = new FileInputStream(new
BufferedOutputStream(temp));
FileOutputStream fos = new FileOutputStream(new
BufferedOutputStream(temp));
byte[] buffer = new byte[dataLen + 2];
int len = -1;
while ((len = raf.read(buffer)) != -1)
fos.wirte(buffer, 0, len);
raf.seek(dataLen);
raf.write(",亭台六七座".getBytes());
// 插入完成后将缓冲的后半部分内容添加进来
while ((len = fis.read(buffer)) != -1)
raf.write(buffer, 0, len);
raf.seek(0);
int fileLen = raf.length();
byte[] data2 = new byte[fileLen + 2];
raf.read(data2, 0, fileLen);
System.out.println(new String(data2));
输出: 烟村四五家......
烟村四五家,亭台六七座......
三、总结:
RandomAccessFile类可以写入和读取文件,其最大的特点就是可以在任意位置读取文件(random access的意思),是通过文件指针实现的。