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
- RandomAccessFile(一)
- JavaIO之RandomAccessFile(一)
- RandomAccessFile(转)
- 文件(三)--RandomAccessFile
- RAF(RandomAccessFile)类
- RandomAccessFile
- randomAccessFile
- RandomAccessFile
- randomAccessFile
- RandomAccessFile
- RandomAccessFile
- RandomAccessFile
- RandomAccessFile
- RandomAccessFile
- RandomAccessFile
- RandomAccessFile
- RandomAccessFile
- RandomAccessFile
- HDOJ 1087 Super Jumping! Jumping! Jumping!
- 类与对象(长方柱)
- Hadoop 2.4 完全分布式环境安装与配置及配置信息介绍
- 函数重载
- DojoX DataGrid
- RandomAccessFile(一)
- 【1】【物理/积分】HDU5826 physics
- javaScript的insertAdjacentHTML()和insertAdjacentText()详解
- jQuery控制Hover 给a标签列表添加鼠标移过换色
- hdu 5444 Elven Postman
- 日程表(最小割)
- 交换棋盘两点
- 卡牌游戏
- Hadoop 2.x分布式集群部署