欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用randomaccessfile在文件任意位置寫入數(shù)據(jù)

 更新時間:2014年01月11日 16:19:21   作者:  
Java在文件任意位置寫入數(shù)據(jù)可以使用RandomAccessFile方法來完成,下面看一個簡單的示例就明白了

復制代碼 代碼如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;


public class InsertContent {
    public static void insert(String fileName, long pos, String insertContent) throws IOException{
        File file = File.createTempFile("tmp", null);
        file.deleteOnExit();
        RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        raf.seek(pos);
        byte[] buff = new byte[64];
        int hasRead = 0;
        while((hasRead = raf.read(buff)) > 0){
            fileOutputStream.write(buff);
        }
        raf.seek(pos);
        raf.write(insertContent.getBytes());
        //追加文件插入點之后的內(nèi)容
        while((hasRead = fileInputStream.read(buff)) > 0){
            raf.write(buff, 0, hasRead);
        }
        raf.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
    public static void main(String[] args) throws IOException {
        insert("F:\AttendanceActivity.java", 57, "插入的內(nèi)容rn");
    }
}

相關(guān)文章

最新評論