• 技术文章 >java >java基础

    Java中RandomAccessFile类如何随机访问

    小妮浅浅小妮浅浅2021-05-11 09:30:58原创2515

    本教程操作环境:windows7系统、java10版,DELL G3电脑。

    1、过程

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

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

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

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

    2、实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    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学习指路:Java基础

    专题推荐:java randomaccessfile
    上一篇:Java对象流实现序列化的类 下一篇:Java中内核线程是什么?

    相关文章推荐

    • java使用ParameterizedType实现泛型• java对象创建过程是什么• java类加载器如何理解?• Java中IO流复制文件的方法• Java对象流实现序列化的类

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网