java使用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());
//追加文件插入点之后的内容
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, "插入的内容rn");
}
}
- java中File类的使用方法
- java 中InputStream,String,File之间的相互转化对比
- 详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别
- 浅谈java 中文件的读取File、以及相对路径的问题
- Java Swing组件文件选择器JFileChooser简单用法示例
- java文件操作工具类分享(file文件工具类)
- java中表示一个文件的File类型详解
- 基于java Files类和Paths类的用法(详解)
- 详解Java中的File文件类以及FileDescriptor文件描述类
- java中FileOutputStream中文乱码问题解决办法
- Java用GDAL读写shapefile的方法示例
- java开发之File类详细使用方法介绍
相关文章
springboot解决Class path contains multiple 
这篇文章主要介绍了springboot解决Class path contains multiple SLF4J bindings问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-07-07
解决异常:Invalid keystore format,springboot配置ssl证书格式不合法问题
这篇文章主要介绍了解决异常:Invalid keystore format,springboot配置ssl证书格式不合法问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-03-03
MyBatis中resultMap和resultType的区别详解
这篇文章主要介绍了MyBatis中resultMap和resultType的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07


最新评论