RandomAccessFile(一)

1、向文件中插入内容案例1
原文件
12345678
插入后
123abcsdfsa45678

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class AccessFileUtils {

    /**
     * 向文件中插入内容案例1
     * @param insertIndex
     * @param content
     * @param file
     * 1234567   123abc4567
     */
    public static void main(String[] args) {
        File file = new File("C:\\Users\\Administrator\\Desktop\\ABC.txt");
        int insertIndex = 3;
        String counte = "abcsdfsa";
        play01(file, insertIndex, counte);
    }

    public static void play01(File file, int insertIndex, String counte){
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

            randomAccessFile.seek(insertIndex);
            byte[] bytes = new byte[1024];
            int currentcounte = 0;
            String tempString = "";
            while((currentcounte = randomAccessFile.read(bytes)) != -1){
                tempString += new String(bytes, 0, currentcounte);
            }
            randomAccessFile.seek(insertIndex + counte.length());
            randomAccessFile.writeBytes(tempString);
            randomAccessFile.seek(insertIndex);
            randomAccessFile.writeBytes(counte);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

2、向文件中插入内容案例2

public static void insertContent2(File file, int insertIndex, String content){
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            //判断如果插入位置不在文件长度范围内,则无效,直接return
            if(insertIndex < 0 || insertIndex > randomAccessFile.length()){
                System.out.println("插入位置无效");
                return;
            }
            //randomAccessFile.length() 9
            //bytes.length 2
            //i 8
            //i - bytes.length 6
            //1234567
            byte[] bytes = content.getBytes();
            randomAccessFile.setLength(randomAccessFile.length() + bytes.length);
            for(long i = randomAccessFile.length() - 1; i > bytes.length + insertIndex; i--){
                randomAccessFile.seek(i - bytes.length);
                byte temp = randomAccessFile.readByte();
                randomAccessFile.seek(i);
                randomAccessFile.writeByte(temp);
            }

            randomAccessFile.seek(insertIndex);
            randomAccessFile.write(bytes);
            randomAccessFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3、向文件中插入内容案例3:涉及中文字符

public static void insertContent3(File file, int insertIndex, String content){
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            //判断如果插入位置不在文件长度范围内,则无效,直接return
            if(insertIndex < 0 || insertIndex > randomAccessFile.length()){
                System.out.println("插入位置无效");
                return;
            }
            randomAccessFile.seek(insertIndex);
            byte[] bytes = new byte[1024];

            int currentCount = 0;
            File tempFile = File.createTempFile("CopyTempFile", "txt", new File("E:"));

            RandomAccessFile tempRandomAccessFile = new RandomAccessFile(tempFile, "rw");
            while((currentCount = randomAccessFile.read(bytes)) != -1){
                tempRandomAccessFile.write(bytes, 0, currentCount);
            }
            randomAccessFile.seek(insertIndex);
            randomAccessFile.writeBytes(content);

            randomAccessFile.seek(insertIndex + content.getBytes().length);

            tempRandomAccessFile.seek(0);
            while((currentCount = tempRandomAccessFile.read(bytes)) != -1){
                randomAccessFile.write(bytes, 0, currentCount);
            }
            randomAccessFile.close();
            tempRandomAccessFile.close();
            tempFile.deleteOnExit();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值