我的代码实现了在文件的头尾处各添加了一行字符串
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
public class 合并文件 {
public static void main(String[] args) throws Exception {
System.out.println("文件大小:" + mergeFiles("c:\\readFilePath.gif", "c:\\222.gif"));//第一个是原始文件路径,第一个是最终生成的文件路径
}
public static long mergeFiles(String readFilePath, String modifyFilePath) throws Exception{
StringBuilder head = new StringBuilder();
head.append("author:hjgzj time:2018-03-05 09:46:07\r\n");//给文件开头加一行字符串
StringBuilder bottom = new StringBuilder();
bottom.append("\r\nthe file end");//给文件末尾加一行字符串
RandomAccessFile raf = new RandomAccessFile(modifyFilePath, "rw");
raf.seek(0);
raf.writeBytes(head.toString());
InputStream is = new FileInputStream(new File(readFilePath));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1 ) {
raf.write(buffer, 0, len);
}
raf.writeBytes(bottom.toString());
long length = raf.length();
raf.close();
return length;
}
}
合并前:
合并后:
狗屎一样的编辑器!!!!!!!!!!