Java randomaccess追加,Java中RandomAccessFile类如何随机访问

f12dc88b3ffae3bd8ac4e9d3033932c8.png

1、过程

(1)既可以充当一个输入流, 也可以冲淡一个输出流

(2)支持从文件的开头读取、写入

(3)支持从任意位置的读取、写入(插入)

(4)RandomAccessFile类需要指定的访问模式:

2、实例public void RandomAccessFile(String src, String srcMode, String dest, String destMode) {

RandomAccessFile accessFile = null;

RandomAccessFile accessFile1 = null;

try {

accessFile = new RandomAccessFile(new File(src), srcMode);

accessFile = new RandomAccessFile(new File(dest), destMode);

byte[] bytes = new byte[1024];

int length;

while ((length = accessFile.read(bytes)) != -1) {

accessFile1.write(bytes, 0, length);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (accessFile != null)

try {

accessFile.close();

} catch (IOException e) {

e.printStackTrace();

}

if (accessFile1 != null) {

try {

accessFile1.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

以上就是Java中RandomAccessFile类随机访问的方法,希望能对大家有所帮助。更多Java学习指路:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值