1.随机访问文件基础知识
package day06file;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* java.io.RandomAccessFile 该类是基于指针形式读取文件数据的
*/
public class RandomAccesFileDemo {
public static void main(String[] args) throws IOException {
/**
* RandomAccessFile的两种创建模式 r:只读模式,仅对文件数据进行读取模式 rw:读写模式,可以对文件数据进行读写操作
* RandomAccessFile的第一参数可以传入字符串(文件路径),也可以传入file对象
*/
RandomAccessFile raf = new RandomAccessFile("raf.dat", "rw");
raf.write(1);
System.out.println("写入完毕");
raf.close();
RandomAccessFile r = new RandomAccessFile("raf.dat", "r");
int d = r.read();
System.out.println(d);
}
}
2.随机访问文件指针
package day06fileraf
import java.io.IOException
import java.io.RandomAccessFile
public class RAFPointer {
public static void main(String[] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile("raf.dat","rw")
long pos = raf.getFilePointer()
System.out.println("pos:"+pos)
int imax = Integer.MAX_VALUE
raf.write(imax>>>24)
pos = raf.getFilePointer()
System.out.println("pos:"+pos)
raf.write(imax>>>16)
pos = raf.getFilePointer()
System.out.println("pos:"+pos)
raf.write(imax>>>8)
pos = raf.getFilePointer()
System.out.println("pos:"+pos)
raf.write(imax)
pos = raf.getFilePointer()
System.out.println("pos:"+pos)
raf.writeInt(imax)
pos = raf.getFilePointer()
System.out.println("pos:"+pos)
//写入long
raf.writeLong(1234556L)
pos = raf.getFilePointer()
System.out.println("pos:"+pos)
//写入double
raf.writeDouble(123.123)
pos = raf.getFilePointer()
System.out.println("pos:"+pos)
//最终查看raf.dat是24个字节
int b = raf.read()
System.out.println(b)
// b = raf.readInt()
//将指针 移动到文件的第一个字节处
raf.seek(0)
b = raf.readInt()
System.out.println(b)
System.out.println("pos:"+raf.getFilePointer())
b = raf.readInt()
System.out.println(b)
System.out.println("pos:"+raf.getFilePointer())
long l = raf.readLong()
System.out.println(l)
System.out.println("pos:"+raf.getFilePointer())
double d = raf.readDouble()
System.out.println(d)
System.out.println("pos:"+raf.getFilePointer())
//再读一次double
raf.seek(16)
d = raf.readDouble()
System.out.println(d)
System.out.println("pos:"+raf.getFilePointer())
System.out.println("写入完毕")
raf.close()
}
}
3.使用随机访问文件实现复制文件
package day06file;
import java.io.IOException;
import java.io.RandomAccessFile;
/** 使用RandomAccessFile实现复制文件
* */
public class RandomAccessFileCopy {
public static void main(String[] args) throws IOException {
RandomAccessFile src = new RandomAccessFile("raf.dat", "r");
RandomAccessFile des = new RandomAccessFile("raf_1.dat", "rw");
int len = -1;
while ((len = src.read()) != -1) {
des.write(len);
}
System.out.println("按照单字节方式复制完毕");
src.close();
des.close();
RandomAccessFile src1 = new RandomAccessFile("raf.dat", "r");
RandomAccessFile des1 = new RandomAccessFile("raf_2.dat", "rw");
int ln = -1;
byte[] buf = new byte[1024*10];
while((ln=src1.read(buf))!=-1){
des1.write(buf, 0, ln);
}
System.out.println("按照字节数组方式复制完毕");
src1.close();
des1.close();
}
}
4.简述RandomAccessFile类的read方法和write方法使用int类型存储byte数据的方式和原因
1)RandomAccessFile提供了一个可以从文件中读取字节的方法:
int read()
该方法会从文件中读取一个byte(8位) 填充到int的低八位, 高24位为0, 返回值范围正数: 0~255, 如果返回-1表示读取到了文件末尾! 每次读取后自动移动文件指针, 准备下次读取。
2)RandomAccessFile提供了一个可以向文件中写出字节的方法:
void write(int d)
该方法会根据当前指针所在位置处写入一个字节,是将参数int的”低8位”写出。
3)使用int类型存储byte数据,这是因为,RandomAccessFile类的read方法,能读到的数据有257个数