7.2 随机文件流

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++;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值