RandomAccessFile 操作文件


文件追加;读取;指定位置(不覆盖)写入内容

package com.example.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;

public class RafUtil {

    /**
     * 在文件指定位置写入内容,不覆盖原来的内容;
     * 先将指定位置后的内容复制到临时文件,将新内容写入后,再将原来内容复制回来
     * @param file 要操作的文件
     * @param off 指定位置/偏移量
     * @param bytes 要写入的内容转换为byte数组
     */
    public static void write(File file, long off, byte[] bytes) {
        System.out.println(off);
        try {
            File tempFile = File.createTempFile("temp_write_" + file.getName(), ".txt");
            FileOutputStream tmpOut = new FileOutputStream(tempFile);
            FileInputStream tmpIn = new FileInputStream(tempFile);
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            randomAccessFile.seek(off);
            byte[] buff = new byte[64];
            int read = 0;
            while ((read = randomAccessFile.read(buff)) > 0) {
                tmpOut.write(buff, 0, read);
            }
            // reset
            randomAccessFile.seek(off);
            randomAccessFile.write(bytes);
            while ((read = tmpIn.read(buff)) > 0) {
                randomAccessFile.write(buff, 0, read);

            }
            randomAccessFile.close();
            tmpIn.close();
            tmpOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 按行读取文件,需要注意编码转换
     * @param file 要读取的文件
     */
    public static void read(File file) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            String line = null;
            while (null != (line = randomAccessFile.readLine())){
                System.out.println(new String(line.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
                System.out.println("  " + randomAccessFile.getFilePointer());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向文件追加内容
     * @param file 要操作的文件
     * @param bytes 要追加的内容
     */
    public static void append(File file, byte[] bytes){
        try {
            RandomAccessFile accessFile =  new RandomAccessFile(file, "rw");
            long length = accessFile.length();
            accessFile.seek(length);
            accessFile.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 获取指定范围的内容
     * @param file 要读取的文件
     * @param begin 开始位置
     * @param end 结束位置
     * @return 指定范围的内容的byte数组
     */
    public static byte[] getBytes(File file, long begin, long end){

        byte[] bytes = null;
        try {
            RandomAccessFile accessFile = new RandomAccessFile(file, "rw");
            accessFile.seek(begin);
            int len = Long.valueOf(end - begin).intValue();
            byte[] buff = new byte[len];
            accessFile.readFully(buff, 0, len);
            bytes = buff;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bytes;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值