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();
}
}