RandomAccessFile
java提供了一个可以对文件随机访问的操作,访问包括读和写操作。
该类名为RandomAccessFile。该类的读写是基于指针的操作。
RandomAccessFile在对文件进行随机访问操作时有两个模式,
分别为只读模式(r),和读写模式(rw)
在创建RandomAccessFile时,需要给传入加两个参数:
1、 操作的文件(文件路径String,File 对象) 如果文件不存在,自动创建
2、 读写模式
public static void main(String[] args) {
try {
RandomAccessFile raf=new RandomAccessFile("data.bat", "rw");
//返回此文件中的当前偏移量。
long point=raf.getFilePointer();
System.out.println(point);
// 4+16*6=96+4
raf.seek(100);
//01111111(1+2+4+8+16+32+64)
//把int的低八位写入到文件中
raf.write(1);
System.out.println("现在的指针位置:"+raf.getFilePointer());
raf.write("i love you".getBytes());
/**
* 读写完毕后要关闭流,释放资源
*/
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 如果读到文件末尾,那么会报EOFException,(End of File),文件末尾返回-1
* @author king-pan
*
*/
public class RandomAccessFileDemo02 {
/**
* @param args
*/
public static void main(String[] args) {
try {
RandomAccessFile raf=new RandomAccessFile("data.bat", "rw");
raf.seek(100);
int i=raf.read();
System.out.println(i);
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件的copy--原理:
/**
* 使用RandomAccessFile实现文件复制
* 步骤:
* 1. 创建一个RandomAccessFile用于读取原文件
* 2. 创建一个RandomAccessFile用于写到目标文件
* 3. 循环一下操作:
* 3.1:从原文件兑取一个字节
* 3.2:将该字节写入到目标文件中
* 退出条件: 当读取的值为-1时,到达文件末尾,结束
*
* 4. 关闭流,释放资源
* @author king-pan
*
*/
public class CopyDemo {
public static void main(String[] args) throws Exception{
RandomAccessFile srcFile=new RandomAccessFile("Linux.txt", "r");
RandomAccessFile descFile=new RandomAccessFile("LinuxCopy.txt", "rw");
int val=-1;
while((val=srcFile.read())!=-1){
descFile.write(val);
}
srcFile.close();
descFile.close();
}
}
文件copy的优化:
/**
* 使用RandomAccessFile实现文件复制(高效率)
* 步骤:
* 1. 创建一个RandomAccessFile用于读取原文件
* 2. 创建一个RandomAccessFile用于写到目标文件
* 3. 循环一下操作:(在读写操作过程中优化)
* 3.1:从原文件兑取一个字节
* 3.2:将该字节写入到目标文件中
* 退出条件: 当读取的值为-1时,到达文件末尾,结束
*
* 4. 关闭流,释放资源
* @author king-pan
*
*/
public class CopyDemo02 {
public static void main(String[] args) throws Exception{
RandomAccessFile srcFile=new RandomAccessFile("Linux.txt", "r");
RandomAccessFile descFile=new RandomAccessFile("LinuxCopy.txt", "rw");
byte [] buf=new byte[1024*10];//数组10kb的容量
/*
* int read(byte[] b)
* 一次性读取byte[]长度个字节,并将这些字节存入该数组。
* 返回值为实际读取道德字节量
* 若返回值为-1,说明这一次没有读到任何字节,也就是文件读到末尾了
*/
int length=-1;
//测试效率 开始时间
long start=System.currentTimeMillis();
while((length=srcFile.read(buf))!=-1){
/*
* 将给定数组中的所有内容写入到文件中
* 最后一次写入可能length小于数组的长度
*/
descFile.write(buf,0,length);
}
//复制完后的时间
long end=System.currentTimeMillis();
System.out.println("耗时:"+(end-start));
srcFile.close();
descFile.close();
}
}
RandomAccessFile对基本类型的读写操作:
public static void write(){
try {
RandomAccessFile raf=new RandomAccessFile("test.bat","rw");
int max=Integer.MAX_VALUE;
/**
* 右移运算
* raf.write(max>>>24);
* raf.write(max>>>16);
* raf.write(max>>>8);
* raf.write(max);
*/
raf.writeByte(Byte.MAX_VALUE);
raf.writeBoolean(Boolean.TRUE);
raf.writeChar(Character.MAX_CODE_POINT);
raf.writeShort(Short.MAX_VALUE);
//完整的写入了一个int
raf.writeInt(max);
raf.writeLong(Long.MAX_VALUE);
raf.writeFloat(Float.MAX_VALUE);
raf.writeDouble(Double.MAX_VALUE);
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void read(){
try {
RandomAccessFile raf=new RandomAccessFile("test.bat","rw");
byte b=raf.readByte();
boolean bl=raf.readBoolean();
char c=raf.readChar();
short s=raf.readShort();
int i=raf.readInt();
long l=raf.readLong();
float f=raf.readFloat();
double d=raf.readDouble();
System.out.println("byte:"+b);
System.out.println("boolean:"+bl);
System.out.println("char:"+c);
System.out.println("short:"+s);
System.out.println("int:"+i);
System.out.println("long:"+l);
System.out.println("float:"+f);
System.out.println("double:"+d);
raf.close();
} catch (Exception e) {
e.printStackTrace();
}
}