2.1 RandomAccessFile类
常用函数(都声明IOException异常类)
public long length() throws IOException // 求随机文件的字节长度
public void seek(long pos) throws IOException// 随机文件记录查找
public void close() throws IOException// 文件关闭
public final double readDouble() ....//读取文件浮点型数
public final int readInt() ....// 读取文件整数的读取
public final char readChar().....// 读取文件字符变量
public int skipBytes(int n) .....// 访问跳过指定字节
用File类和RandomAccessFile类实现文件拷贝
package File07;
import java.io.*;
public class Test71 {
public static String path = "E:\\java\\interface_test\\FileTest\\src\\File07";
public static String filename = "file1.txt";
public static void main(String[] args) throws IOException {
RandomAccessFile file = new RandomAccessFile(path+File.separator+filename, "r");
File f2 = new File(path,"file2.txt");
f2.createNewFile();
RandomAccessFile file2 = new RandomAccessFile(path + File.separator +"file2.txt","rw");
long i =1;
int n = 2;
while(true){
byte[] buffer = new byte[n];
if(file.read(buffer)==-1) break;
file2.write(buffer);
file.seek(i*n);
file2.seek(i*n);
i++;
}
}
}