Home >Java >javaTutorial >How to access randomly in Java's RandomAccessFile class

How to access randomly in Java's RandomAccessFile class

WBOY
WBOYforward
2023-04-28 09:31:061052browse

1. Process

(1) Can act as an input stream or dilute an output stream

(2) Supports reading from the beginning of the file , write

(3) Support reading and writing (insertion) from any location

(4) The RandomAccessFile class needs to specify the access mode:

2. Example

    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();
                }
            }
        }
    }

The above is the detailed content of How to access randomly in Java's RandomAccessFile class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete