跳到文件指定位置加入版本信息
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();
}
}
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();
}
}
相关文章推荐
- 搭建Struts2开发环境并整合Spring3
- 搭建Spring3开发环境
- Tiny并行计算框架之使用介绍
- Tiny并行计算框架之实现机理
- iOS单例设计模式详解教程
- Tomcat启动不了
- 使用Maven搭建Struts2+Spring3+Hibernate4的整合开发环境
- Event.target
- redhat6 GCC安装
- COM学习笔记8_IDispatch (调度接口) 自动化
- Win10 10125中文语言包安装出现乱码的解决方法
- 【剑指offer】 面试题8: 旋转数组的最小数字
- chrome浏览器插件开发标准格式
- iOS-集成环信
- 《 Web应用测试 》笔记(第4 章-网络基础)
- 页面中调用函数--之${fn:}内置函数
- Android学习 1 -构建运行环境
- USB WiFi网卡驱动分析--经典
- 怎样使用OpenCV进行人脸识别
- JSP基本用法(二)隐含对象