RandomAccessFile
1. java.io.RandomAccessFile
java.io.RandomAccessFile是用来读写文件的API,其基于指针可以对文件任意位置进行读写操作
- 例如当前目录下的raf.dat文件操作
- 其有两个构造方法
- RandomAccessFile(String path,String mode)
- 第一个参数为要操作文件的路径
* - RandomAccessFile(File file,String mode)
- 第一个参数是要操作的文件所表示的file对象
* - 第二个参数有两个值:r/rw
- r:只读模式,对文件做读取操作
- rw:读写模式,对文件数据既可读又可写
* - 注释:如果指定的文件不存在,那么当操作模式为读写模式 时,会自动创建该文件。如果是只读模式则会出现异常(系统找不到路径)
RandomAccessFile raf = new RandomAccessFile("./raf.dat","rw");
2. 创建一个文件并且向其中写入字节
向文件中写入1个字节void writer(int d)
写入的是给定的int值所对应的的2进制的“低八位”(对于负数也是如此)
* 整数1,二进制:
* 00000000 00000000 00000000 00000001
RandomAccessFile raf = new RandomAccessFile("./raf.dat","rw");
raf.write(1);//00000001;
raf.write(2);//00000010;
raf.write(13;//00001101;
System.out.println("写出完毕!");
raf.close();//读写完毕后必须关闭
多次调用write方法,可以写入多个字节,每一次运行都是覆盖重写
3.读取文件中的字节(一次一个,依次读取)
int read()读取一个字节(只有低八位),并以int形式返回,若返回的int值为-1则表示文件末尾
RandomAccessFile raf = new RandomAccessFile("./raf.dat","r");
/*
* raf.dat文件中的内容:
* 00000001 00000010 00001101
*/
int d = raf.read();
System.out.println(d);//1
d = raf.read();
System.out.println(d);//2
d = raf.read();
System.out.println(d);//13
d = raf.read();
System.out.println(d);//-1,end
raf.close();
4.基于指针的读写操作和读写基本数据类型
long getFilePointer()获取当前指针位置
void seek(long pos)移动指针到指定位置
方便读取基本类型数据的方法:int readInt() 连续读取4字节,还原对应的int值
RandomAccessFile raf = new RandomAccessFile("raf.dat","rw");
long point =raf.getFilePointer();
/*
* long getFilePointer()
* 获取当前指针位置
*/
System.out.println("pos:"+point);
/*
* 将int最大值写入文件
* int最大值的2进制:
* ********
* 01111111 11111111 11111111 11111111
* max>>>24
* ********
* 00000000 00000000 00000000 01111111
*/
int max = Integer.MAX_VALUE ;
raf.write(max>>>24);
raf.write(max>>>16);
raf.write(max>>>8);
raf.write(max);
System.out.println("pos:"+raf.getFilePointer());
raf.writeInt(max);//连续写四个字节,将int值写出等同于上面四句
System.out.println("pos:"+raf.getFilePointer());
raf.writeLong(123L);//连续写八个字节,将long值写出
System.out.println("pos:"+raf.getFilePointer());
raf.writeDouble(123.456);;
System.out.println("pos:"+raf.getFilePointer());
System.out.println("end");
/*
* void seek(long pos)
* 移动指针到指定位置
*
*/
raf.seek(0);
System.out.println("pos:"+raf.getFilePointer());//0
/*
* RAF也提供了方便读取基本类型数据的方法
* int readInt()
* 连续读取4字节,还原对应的int值
*/
int num = raf.readInt();
System.out.println(num);
System.out.println("pos:"+raf.getFilePointer());
raf.seek(8);
long ll = raf.readLong();
System.out.println(ll);
raf.seek(16);
double dou = raf.readDouble();
System.out.println(dou);
/*
* 将long值修改为666
*/
//1. 先将指针移动到long的第一个字节的位置
raf.seek(8);
//2. 重新写入8字节,将整数66对应的2进制的8个字节写入文件覆盖位置
long lo = 666L;
raf.writeLong(lo);
raf.close();
5. 文件写入字符串
byte getBytes()将当前字符串按照系统默认的字符串集转换为一组字节
byte getBytes(String csn)将当前字符串按照指定的字符串集转换为一组字节
RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
String str = "asdasd";
byte[]data = str .getBytes("utf-8");
raf.write(data);
str = "油楠数";
data = str.getBytes("utf-8");
raf.write(data);
System.out.println("写出完毕");
raf.close();
相对路径中的“./"可以不写,不写就是默认./
6. 读取字符串
String提供的构造方法:
String(byte[]data,String csn)
将给定的字节数组中所有字节按照给定的字符集还原为字符串
RandomAccessFile raf = new RandomAccessFile("raf.txt","r");
byte[]data = new byte[(int)raf.length()];
raf.read(data);
String str = new String(data,"utf-8");
System.out.println(str);
raf.close();
7.对文件的复制
7.1 一次性将给定的字节数组的所有字节写入文件
int read(byte[] data)
一次性读取给定的字节数组总长度的字节量,并将读取的字节放入该数组中,此时返回值表示实际读取到的字节数,如果返回值为-1则表示文件末尾
注意:
这里的返回值int不再是实际读取到的字节内容,而仅表示读取到的字节数,实际读取的内容存在数组里了
RandomAccessFile src = new RandomAccessFile("./src/WechatIMG813.png","r");
RandomAccessFile desc = new RandomAccessFile("./image.png","rw");
int f = 0;
long start = System.currentTimeMillis();
while((f = src.read())!=-1){
desc.write(f);
}
long end = System.currentTimeMillis();
System.out.println("copy over! use time:"+(end-start));
src.close();
desc.close();
7.2将给定的字节数组从指定下标处的连续指定数量的字节一次性写入文件
void write(byte[] data, int start,int len) ;将给定的字节数组从下标start处的连续len个字节一次性写入文件
- 8位2进制 1byte
- 1024byte 1kb
- 1024kb 1mb
- 1924mb 1gb
- 1024gb 1tb
RandomAccessFile src = new RandomAccessFile("./noveie_cp.mp4","r");
RandomAccessFile desc = new RandomAccessFile("./moveie_cp.mp4","rw");
byte[]buf = new byte[1024*10];//10kb
int len = 0;//记录的是每次读取到的字节数
long start = System.currentTimeMillis();
while((len = src.read(buf))!=-1){
desc.write(buf,0,len);//此处的0为buf从下标0开始
}
long end = System.currentTimeMillis();
System.out.println("copy over! usetime:"+(end-start)+"ms");
src.close();
desc.close();