RandomAccessFile(一)

来源:互联网 发布:2017网络热曲 编辑:程序博客网 时间:2024/05/21 17:45

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电信dns辅服务器未响应怎么办 笔记本wifi下载速度慢怎么办 苹果wifi下载速度慢怎么办 小米手机wifi下载速度慢怎么办 苹果8plus上网慢怎么办 小米5c死机了怎么办 苹果x自拍反方向怎么办 硅胶手机壳出油怎么办 指环扣松了怎么办图解 塑料放久了发粘怎么办 橡胶时间久了粘怎么办 胶的手机套变黄怎么办 手机壳硅胶变黄怎么办 硅胶手机壳大了怎么办 硅胶手机壳变大了怎么办 硅胶手机壳有点大怎么办 硅胶手机壳粘手怎么办 透明手机壳变黄怎么办? 耳机胶套经常掉怎么办 硅胶手机壳粘毛怎么办 耳机海绵套坏了怎么办 沙发垫海绵坏了怎么办 汽车坐海绵坏了怎么办 苹果手机边框有缝隙怎么办 孕期牙套子掉了怎么办 平果充电线不匹配怎么办 苹果六外壳掉漆怎么办 苹果平板充电没反应怎么办 苹果平板黑屏没反应怎么办 苹果平板卡机了怎么办 苹果7plus掉漆怎么办 皮的手机壳脏了怎么办 小米5x边边裂开怎么办 荣耀8的后盖摔了怎么办 hp打印机卡了纸怎么办 华为荣耀9进水了怎么办 小米5x屏幕脱胶怎么办 小米5x后盖松动怎么办 苹果手机没电了怎么办 荣耀9的后盖裂了怎么办 手机一直在开机画面怎么办华为