public class ByteArrayOutputStreamTest {
@Test
public void test() {
RandomAccessFile raf = null;
ByteArrayOutputStream bos = null;
try {
raf = new RandomAccessFile(new File("ByteArrayOutputStreamTest"), "rw");
bos = new ByteArrayOutputStream();
raf.seek(3);
byte[] buffer = new byte[20];
int len;
while ((len = raf.read(buffer)) != -1) {
bos.write(buffer,0,len);//将从索引位置3开始到后面的所有数据先放在ByteArrayOutputStream中
}
String s = "lihai";
raf.seek(3);//将指针重新指回到3索引位置!
raf.write(s.getBytes());
raf.write(bos.toString().getBytes());//在字节数组缓冲区数据直接写到文件中去!完成了插入数据的操作
} catch (IOException e) {
e.printStackTrace();
} finally {
if (raf != null) {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
ByteArrayOutputStream
最新推荐文章于 2024-03-18 21:56:54 发布