您的位置:首页 > 其它

跳到文件指定位置加入版本信息

2015-06-09 16:02 260 查看
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);

byte[] data = insertContent.getBytes();

System.out.println(data.length);

raf.write(data.length);

raf.write(data);

// 追加文件插入点之后的内容

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("zdy.h", 346112, "alpha1s_V1.0");

// File file = new File("zdy.h");

// System.out.println(file.length());

writeIntoFIle("zdy.h");

}

public static void writeIntoFIle(String filePath) throws IOException {

File newFile = new File(filePath);

RandomAccessFile raf = new RandomAccessFile(newFile, "rw");

raf.seek(346112);

int i = raf.read();

byte[] d = new byte[i];

raf.read(d, 0, i);

String x = new String(d);

System.out.println(x);

raf.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: