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类怎么随机访问的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号