Java中RandomAccessFile类怎么随机访问

这篇文章主要介绍“Java中RandomAccessFile类怎么随机访问”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java中RandomAccessFile类怎么随机访问”文章能帮助大家解决问题。

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类怎么随机访问”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程之家行业资讯频道,小编每天都会为大家更新不同的知识点。

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...